File tree Expand file tree Collapse file tree 1 file changed +16
-8
lines changed
openlibrary/plugins/openlibrary Expand file tree Collapse file tree 1 file changed +16
-8
lines changed Original file line number Diff line number Diff line change @@ -107,18 +107,26 @@ def GET(self):
107107 return web .template .TemplateResult (cached_homepage )
108108
109109
110+ @cache .memoize (
111+ engine = "memcache" , key = "home.random_book" , expires = dateutil .HALF_HOUR_SECS
112+ )
113+ def get_random_borrowable_ebook_keys (count : int ) -> list [str ]:
114+ solr = search .get_solr ()
115+ docs = solr .select (
116+ 'type:edition AND ebook_access:[borrowable TO *]' ,
117+ fields = ['key' ],
118+ rows = count ,
119+ sort = f'random_{ random .random ()} desc' ,
120+ )['docs' ]
121+ return [doc ['key' ] for doc in docs ]
122+
123+
110124class random_book (delegate .page ):
111125 path = "/random"
112126
113127 def GET (self ):
114- solr = search .get_solr ()
115- key = solr .select (
116- 'type:edition AND ebook_access:[borrowable TO *]' ,
117- fields = ['key' ],
118- rows = 1 ,
119- sort = f'random_{ random .random ()} desc' ,
120- )['docs' ][0 ]['key' ]
121- raise web .seeother (key )
128+ keys = get_random_borrowable_ebook_keys (1000 )
129+ raise web .seeother (random .choice (keys ))
122130
123131
124132def get_ia_carousel_books (query = None , subject = None , sorts = None , limit = None ):
You can’t perform that action at this time.
0 commit comments