@@ -16,6 +16,7 @@ import (
1616 "github.com/gorilla/mux"
1717 httprange "github.com/gotd/contrib/http_range"
1818 "github.com/pkg/errors"
19+ "github.com/readium/go-toolkit/cmd/rwp/cmd/serve/cache"
1920 "github.com/readium/go-toolkit/pkg/asset"
2021 "github.com/readium/go-toolkit/pkg/manifest"
2122 "github.com/readium/go-toolkit/pkg/pub"
@@ -53,43 +54,49 @@ func (s *Server) getPublication(filename string) (*pub.Publication, error) {
5354 return nil , err
5455 }
5556
56- // TODO: cache open publications
57-
5857 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+ }
6566
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 )
8588 }
86- makeCollectionRelative (mp [i ][j ].Subcollections )
8789 }
8890 }
89- }
90- makeCollectionRelative (pub .Manifest .Subcollections )
91+ makeCollectionRelative (pub .Manifest .Subcollections )
9192
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
93100}
94101
95102func (s * Server ) getManifest (w http.ResponseWriter , req * http.Request ) {
0 commit comments