View Single Post
Old 10-22-2020, 03:07 PM   #10
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,203
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by chaley View Post
You have a custom (Python) template function? If so, then yes you can cache results. The reason: each function is compiled into its own persistent class. You can store stuff as class attributes, which will persist until the next time calibre restarts, you change the library, or are running the function in a separate job.

The approach is to check if the desired cache class attribute exists. If so, use it. If not then fetch and store it, then use it. Sample code:
Code:
	x = getattr(self, 'foobar', None)
	if x is None:
		self.foobar = {'element': 'value'} # do what it takes to set foobar's value
	# use self.foobar in whatever way you want
It is up to you to invalidate the attribute (the 'cache') when needed. Simply restarting calibre will do it.
That should do it.
capink is offline   Reply With Quote