@@ -16,6 +16,7 @@ import (
16
16
"github.com/gorilla/mux"
17
17
httprange "github.com/gotd/contrib/http_range"
18
18
"github.com/pkg/errors"
19
+ "github.com/readium/go-toolkit/cmd/rwp/cmd/serve/cache"
19
20
"github.com/readium/go-toolkit/pkg/asset"
20
21
"github.com/readium/go-toolkit/pkg/manifest"
21
22
"github.com/readium/go-toolkit/pkg/pub"
@@ -53,43 +54,49 @@ func (s *Server) getPublication(filename string) (*pub.Publication, error) {
53
54
return nil , err
54
55
}
55
56
56
- // TODO: cache open publications
57
-
58
57
cp := filepath .Clean (string (fpath ))
59
- pub , err := streamer .New (streamer.Config {
60
- InferA11yMetadata : s .config .InferA11yMetadata ,
61
- }).Open (asset .File (filepath .Join (s .config .BaseDirectory , cp )), "" )
62
- if err != nil {
63
- return nil , errors .Wrap (err , "failed opening " + cp )
64
- }
58
+ dat , ok := s .lfu .Get (cp )
59
+ if ! ok {
60
+ pub , err := streamer .New (streamer.Config {
61
+ InferA11yMetadata : s .config .InferA11yMetadata ,
62
+ }).Open (asset .File (filepath .Join (s .config .BaseDirectory , cp )), "" )
63
+ if err != nil {
64
+ return nil , errors .Wrap (err , "failed opening " + cp )
65
+ }
65
66
66
- // TODO: Remove this after we make links relative in the go-toolkit
67
- for i , link := range pub .Manifest .Links {
68
- pub .Manifest .Links [i ] = makeRelative (link )
69
- }
70
- for i , link := range pub .Manifest .Resources {
71
- pub .Manifest .Resources [i ] = makeRelative (link )
72
- }
73
- for i , link := range pub .Manifest .ReadingOrder {
74
- pub .Manifest .ReadingOrder [i ] = makeRelative (link )
75
- }
76
- for i , link := range pub .Manifest .TableOfContents {
77
- pub .Manifest .TableOfContents [i ] = makeRelative (link )
78
- }
79
- var makeCollectionRelative func (mp manifest.PublicationCollectionMap )
80
- makeCollectionRelative = func (mp manifest.PublicationCollectionMap ) {
81
- for i := range mp {
82
- for j := range mp [i ] {
83
- for k := range mp [i ][j ].Links {
84
- mp [i ][j ].Links [k ] = makeRelative (mp [i ][j ].Links [k ])
67
+ // TODO: Remove this after we make links relative in the go-toolkit
68
+ for i , link := range pub .Manifest .Links {
69
+ pub .Manifest .Links [i ] = makeRelative (link )
70
+ }
71
+ for i , link := range pub .Manifest .Resources {
72
+ pub .Manifest .Resources [i ] = makeRelative (link )
73
+ }
74
+ for i , link := range pub .Manifest .ReadingOrder {
75
+ pub .Manifest .ReadingOrder [i ] = makeRelative (link )
76
+ }
77
+ for i , link := range pub .Manifest .TableOfContents {
78
+ pub .Manifest .TableOfContents [i ] = makeRelative (link )
79
+ }
80
+ var makeCollectionRelative func (mp manifest.PublicationCollectionMap )
81
+ makeCollectionRelative = func (mp manifest.PublicationCollectionMap ) {
82
+ for i := range mp {
83
+ for j := range mp [i ] {
84
+ for k := range mp [i ][j ].Links {
85
+ mp [i ][j ].Links [k ] = makeRelative (mp [i ][j ].Links [k ])
86
+ }
87
+ makeCollectionRelative (mp [i ][j ].Subcollections )
85
88
}
86
- makeCollectionRelative (mp [i ][j ].Subcollections )
87
89
}
88
90
}
89
- }
90
- makeCollectionRelative (pub .Manifest .Subcollections )
91
+ makeCollectionRelative (pub .Manifest .Subcollections )
91
92
92
- return pub , nil
93
+ // Cache the publication
94
+ encPub := & cache.CachedPublication {Publication : pub }
95
+ s .lfu .Set (cp , encPub )
96
+
97
+ return encPub .Publication , nil
98
+ }
99
+ return dat .(* cache.CachedPublication ).Publication , nil
93
100
}
94
101
95
102
func (s * Server ) getManifest (w http.ResponseWriter , req * http.Request ) {
0 commit comments