Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
bahn-api
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Janne Mareike Koschinski
bahn-api
Commits
dd91306f
Verified
Commit
dd91306f
authored
6 years ago
by
Janne Mareike Koschinski
Browse files
Options
Downloads
Patches
Plain Diff
Implement caching
parent
4e4edf2d
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
cache.go
+6
-0
6 additions, 0 deletions
cache.go
client.go
+145
-0
145 additions, 0 deletions
client.go
with
151 additions
and
0 deletions
cache.go
0 → 100644
+
6
−
0
View file @
dd91306f
package
bahn
type
CacheBackend
interface
{
Set
(
key
string
,
value
interface
{})
error
Get
(
key
string
,
value
interface
{})
error
}
This diff is collapsed.
Click to expand it.
client.go
+
145
−
0
View file @
dd91306f
...
...
@@ -17,9 +17,28 @@ type ApiClient struct {
CoachSequenceBaseUrl
string
HafasBaseUrl
string
HttpClient
*
http
.
Client
Caches
[]
CacheBackend
}
func
(
c
*
ApiClient
)
Station
(
evaId
int64
)
([]
Station
,
error
)
{
key
:=
fmt
.
Sprintf
(
"realtime_recent %d"
,
evaId
)
var
result
[]
Station
for
_
,
cache
:=
range
c
.
Caches
{
if
err
:=
cache
.
Get
(
key
,
result
);
err
==
nil
{
return
result
,
nil
}
}
var
err
error
if
result
,
err
=
c
.
loadStation
(
evaId
);
err
==
nil
{
for
_
,
cache
:=
range
c
.
Caches
{
_
=
cache
.
Set
(
key
,
result
)
}
}
return
result
,
err
}
func
(
c
*
ApiClient
)
loadStation
(
evaId
int64
)
([]
Station
,
error
)
{
var
err
error
uri
:=
fmt
.
Sprintf
(
"%s/timetable/station/%d"
,
c
.
IrisBaseUrl
,
evaId
)
...
...
@@ -43,6 +62,24 @@ func (c *ApiClient) Station(evaId int64) ([]Station, error) {
}
func
(
c
*
ApiClient
)
Timetable
(
evaId
int64
,
date
time
.
Time
)
(
Timetable
,
error
)
{
key
:=
fmt
.
Sprintf
(
"timetable %d %s"
,
evaId
,
date
)
var
result
Timetable
for
_
,
cache
:=
range
c
.
Caches
{
if
err
:=
cache
.
Get
(
key
,
result
);
err
==
nil
{
return
result
,
nil
}
}
var
err
error
if
result
,
err
=
c
.
loadTimetable
(
evaId
,
date
);
err
==
nil
{
for
_
,
cache
:=
range
c
.
Caches
{
_
=
cache
.
Set
(
key
,
result
)
}
}
return
result
,
err
}
func
(
c
*
ApiClient
)
loadTimetable
(
evaId
int64
,
date
time
.
Time
)
(
Timetable
,
error
)
{
var
err
error
BahnFormat
:=
"060102/15"
...
...
@@ -67,6 +104,24 @@ func (c *ApiClient) Timetable(evaId int64, date time.Time) (Timetable, error) {
}
func
(
c
*
ApiClient
)
RealtimeAll
(
evaId
int64
,
date
time
.
Time
)
(
Timetable
,
error
)
{
key
:=
fmt
.
Sprintf
(
"realtime_all %d %s"
,
evaId
,
date
)
var
result
Timetable
for
_
,
cache
:=
range
c
.
Caches
{
if
err
:=
cache
.
Get
(
key
,
result
);
err
==
nil
{
return
result
,
nil
}
}
var
err
error
if
result
,
err
=
c
.
loadRealtimeAll
(
evaId
,
date
);
err
==
nil
{
for
_
,
cache
:=
range
c
.
Caches
{
_
=
cache
.
Set
(
key
,
result
)
}
}
return
result
,
err
}
func
(
c
*
ApiClient
)
loadRealtimeAll
(
evaId
int64
,
date
time
.
Time
)
(
Timetable
,
error
)
{
var
err
error
uri
:=
fmt
.
Sprintf
(
"%s/timetable/fchg/%d"
,
c
.
IrisBaseUrl
,
evaId
)
...
...
@@ -90,6 +145,24 @@ func (c *ApiClient) RealtimeAll(evaId int64, date time.Time) (Timetable, error)
}
func
(
c
*
ApiClient
)
RealtimeRecent
(
evaId
int64
,
date
time
.
Time
)
(
Timetable
,
error
)
{
key
:=
fmt
.
Sprintf
(
"realtime_recent %d %s"
,
evaId
,
date
)
var
result
Timetable
for
_
,
cache
:=
range
c
.
Caches
{
if
err
:=
cache
.
Get
(
key
,
result
);
err
==
nil
{
return
result
,
nil
}
}
var
err
error
if
result
,
err
=
c
.
loadRealtimeRecent
(
evaId
,
date
);
err
==
nil
{
for
_
,
cache
:=
range
c
.
Caches
{
_
=
cache
.
Set
(
key
,
result
)
}
}
return
result
,
err
}
func
(
c
*
ApiClient
)
loadRealtimeRecent
(
evaId
int64
,
date
time
.
Time
)
(
Timetable
,
error
)
{
var
err
error
uri
:=
fmt
.
Sprintf
(
"%s/timetable/rchg/%d"
,
c
.
IrisBaseUrl
,
evaId
)
...
...
@@ -113,6 +186,24 @@ func (c *ApiClient) RealtimeRecent(evaId int64, date time.Time) (Timetable, erro
}
func
(
c
*
ApiClient
)
WingDefinition
(
parent
string
,
wing
string
)
(
WingDefinition
,
error
)
{
key
:=
fmt
.
Sprintf
(
"wing_definition %s %s"
,
parent
,
wing
)
var
result
WingDefinition
for
_
,
cache
:=
range
c
.
Caches
{
if
err
:=
cache
.
Get
(
key
,
result
);
err
==
nil
{
return
result
,
nil
}
}
var
err
error
if
result
,
err
=
c
.
loadWingDefinition
(
parent
,
wing
);
err
==
nil
{
for
_
,
cache
:=
range
c
.
Caches
{
_
=
cache
.
Set
(
key
,
result
)
}
}
return
result
,
err
}
func
(
c
*
ApiClient
)
loadWingDefinition
(
parent
string
,
wing
string
)
(
WingDefinition
,
error
)
{
var
err
error
uri
:=
fmt
.
Sprintf
(
"%s/timetable/wingdef/%s/%s"
,
c
.
IrisBaseUrl
,
parent
,
wing
)
...
...
@@ -136,6 +227,24 @@ func (c *ApiClient) WingDefinition(parent string, wing string) (WingDefinition,
}
func
(
c
*
ApiClient
)
CoachSequence
(
line
string
,
date
time
.
Time
)
(
CoachSequence
,
error
)
{
key
:=
fmt
.
Sprintf
(
"coach_sequence %s %s"
,
line
,
date
.
String
())
var
result
CoachSequence
for
_
,
cache
:=
range
c
.
Caches
{
if
err
:=
cache
.
Get
(
key
,
result
);
err
==
nil
{
return
result
,
nil
}
}
var
err
error
if
result
,
err
=
c
.
loadCoachSequence
(
line
,
date
);
err
==
nil
{
for
_
,
cache
:=
range
c
.
Caches
{
_
=
cache
.
Set
(
key
,
result
)
}
}
return
result
,
err
}
func
(
c
*
ApiClient
)
loadCoachSequence
(
line
string
,
date
time
.
Time
)
(
CoachSequence
,
error
)
{
var
err
error
uri
:=
fmt
.
Sprintf
(
"%s/%s/%s"
,
c
.
CoachSequenceBaseUrl
,
line
,
date
.
Format
(
TimeLayoutShort
))
...
...
@@ -159,6 +268,24 @@ func (c *ApiClient) CoachSequence(line string, date time.Time) (CoachSequence, e
}
func
(
c
*
ApiClient
)
Suggestions
(
line
string
,
date
time
.
Time
)
([]
Suggestion
,
error
)
{
key
:=
fmt
.
Sprintf
(
"suggestions %s %s"
,
line
,
date
.
String
())
var
result
[]
Suggestion
for
_
,
cache
:=
range
c
.
Caches
{
if
err
:=
cache
.
Get
(
key
,
result
);
err
==
nil
{
return
result
,
nil
}
}
var
err
error
if
result
,
err
=
c
.
loadSuggestions
(
line
,
date
);
err
==
nil
{
for
_
,
cache
:=
range
c
.
Caches
{
_
=
cache
.
Set
(
key
,
result
)
}
}
return
result
,
err
}
func
(
c
*
ApiClient
)
loadSuggestions
(
line
string
,
date
time
.
Time
)
([]
Suggestion
,
error
)
{
var
err
error
uri
:=
fmt
.
Sprintf
(
"%s/trainsearch.exe/dn"
,
c
.
HafasBaseUrl
)
...
...
@@ -203,6 +330,24 @@ func (c *ApiClient) Suggestions(line string, date time.Time) ([]Suggestion, erro
}
func
(
c
*
ApiClient
)
HafasMessages
(
trainlink
string
)
([]
HafasMessage
,
error
)
{
key
:=
fmt
.
Sprintf
(
"hafas_messages %s"
,
trainlink
)
var
result
[]
HafasMessage
for
_
,
cache
:=
range
c
.
Caches
{
if
err
:=
cache
.
Get
(
key
,
result
);
err
==
nil
{
return
result
,
nil
}
}
var
err
error
if
result
,
err
=
c
.
loadHafasMessages
(
trainlink
);
err
==
nil
{
for
_
,
cache
:=
range
c
.
Caches
{
_
=
cache
.
Set
(
key
,
result
)
}
}
return
result
,
err
}
func
(
c
*
ApiClient
)
loadHafasMessages
(
trainlink
string
)
([]
HafasMessage
,
error
)
{
var
err
error
uri
:=
fmt
.
Sprintf
(
"%s/traininfo.exe/dn/%s?rt=1&ajax=1"
,
c
.
HafasBaseUrl
,
trainlink
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment