Package: omoide-cache
Omoide Cache - Simple in-memory cache solutionCaching doesn't need to be hard anymore. With just a few lines of codeOmoide Cachewill instantly bring your Python services to the next level!Released Library:PyPISource Code:GitHubTutorial №1:my blogorMediumDescriptionThis is a robust, highly tunable and easy-to-integrate in-memory cache solution written in pure Python, with no dependencies.It is designed to be a method level cache, wrapping around a single class method, using method call arguments as cache key and storing its return value.Customizable to suit your specific use-case, provides various expiry and refresh options.Very user-friendly, super easy to integrate with a simple decorator (i.e. annotation, for those coming from Java), no need to add complicated logic into your code, just use@omoide_cache()on top of any method in your services. It will auto-generate a cache for your method with default settings. You can further adjust these settings through decorator parameters.When to use?You got a heavy call to the data source, where the data is read way more often than it is updated.You have CPU intensive computation logic, that takes a few seconds to complete, but can is frequently called with same input parameters.When not to use?On methods that are not expected to be frequently called with the same arguments (e.g. image processing / OCR / ML models with image inputs)On methods that return new values each time they are called, even with the same arguments.When you expect argument objects or returned objects to take-up a lot of memory. Cache will quickly eat up your ram if you don't setup expiry modes properly.Functions that are declared outside of class is a no go.Fair warning - this project is in the earliest stage of its lifecycle, there will be a lot of improvement and bug fixes in the future. All suggestions and bug reports are highly welcome!InstallationNormal installationpipinstallomoide-cacheDevelopment installationgitclonehttps://github.com/jpleorx/omoide-cache.gitcdomoide-cache
pipinstall--editable.Examples1 - Basic usage exampleimporttimefromomoide_cacheimportomoide_cache# A class where cache was added to a simulated long running methodclassExampleService:@omoide_cache()deftime_consuming_method(self,x:int)->int:time.sleep(2.0)returnx*xservice=ExampleService()# The first call will execute real logic and store the result in cacheservice.time_consuming_method(1)# The second call will get results from cacheservice.time_consuming_method(1)2 - Example with size limitHere we add a cache that will drop an item least frequently accessed when the cache becomes too large.importtimefromomoide_cacheimportomoide_cache,ExpireModeclassExampleService:@omoide_cache(max_allowed_size=10,size_expire_mode=ExpireMode.ACCESS_COUNT_BASED)deftime_consuming_method(self,x:int)->int:time.sleep(2.0)returnx*x3 - Example with timed expiryHere the cache will automatically remove items that were last accessed more than 2 minutes ago.importtimefromomoide_cacheimportomoide_cacheclassExampleService:@omoide_cache(expire_by_access_duration_s=120)deftime_consuming_method(self,x:int)->int:time.sleep(2.0)returnx*xAlternatively we can remove items that were computed more than 2 minutes ago.importtimefromomoide_cacheimportomoide_cacheclassExampleService:@omoide_cache(expire_by_computed_duration_s=120)deftime_consuming_method(self,x:int)->int:time.sleep(2.0)returnx*x4 - Example with async refreshHere the cache will asynchronously refresh items that were computed more than 2 minutes ago. Attempt to refresh will be performed every 10 seconds.importtimefromomoide_cacheimportomoide_cache,RefreshModeclassExampleService:@omoide_cache(refresh_duration_s=120,refresh_period_s=10,refresh_mode=RefreshMode.INDEPENDENT)deftime_consuming_method(self,x:int)->int:time.sleep(2.0)returnx*xKnown bugsYou need to use the decorator with parentheses all the time, even when you don't specify any arguments, so use@omoide_cache(), but not@omoide_cache. I honestly have no fucking idea why there's this weird behaviour in decorators, will do my best to fix it in future updates.LinksIn case you’d like to check my other work or contact me:Personal websiteGitHubPyPIDockerHubArticles on MediumLinkedIn (feel free to connect)