Skip to main content

Omoide Cache

Omoide Cache - Simple in-memory cache solution

Caching doesn't need to be hard anymore. With just a few lines of code Omoide Cache will instantly bring your Python services to the next level!


Released Library: PyPI

Source Code: GitHub

Tutorial №1: my blog or Medium


Description

This 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!

Installation

Normal installation

pip install omoide-cache

Development installation

git clone https://github.com/jpleorx/omoide-cache.git
cd omoide-cache
pip install --editable .

Examples

1 - Basic usage example

import time
from omoide_cache import omoide_cache


# A class where cache was added to a simulated long running method
class ExampleService:
    @omoide_cache()
    def time_consuming_method(self, x: int) -> int:
        time.sleep(2.0)
        return x * x


service = ExampleService()

# The first call will execute real logic and store the result in cache
service.time_consuming_method(1)

# The second call will get results from cache
service.time_consuming_method(1)

2 - Example with size limit

Here we add a cache that will drop an item least frequently accessed when the cache becomes too large.

import time
from omoide_cache import omoide_cache, ExpireMode


class ExampleService:
    @omoide_cache(max_allowed_size=10, size_expire_mode=ExpireMode.ACCESS_COUNT_BASED)
    def time_consuming_method(self, x: int) -> int:
        time.sleep(2.0)
        return x * x

3 - Example with timed expiry

Here the cache will automatically remove items that were last accessed more than 2 minutes ago.

import time
from omoide_cache import omoide_cache


class ExampleService:
    @omoide_cache(expire_by_access_duration_s=120)
    def time_consuming_method(self, x: int) -> int:
        time.sleep(2.0)
        return x * x

Alternatively we can remove items that were computed more than 2 minutes ago.

import time
from omoide_cache import omoide_cache


class ExampleService:
    @omoide_cache(expire_by_computed_duration_s=120)
    def time_consuming_method(self, x: int) -> int:
        time.sleep(2.0)
        return x * x

4 - Example with async refresh

Here the cache will asynchronously refresh items that were computed more than 2 minutes ago. Attempt to refresh will be performed every 10 seconds.

import time
from omoide_cache import omoide_cache, RefreshMode


class ExampleService:
    @omoide_cache(refresh_duration_s=120, refresh_period_s=10, refresh_mode=RefreshMode.INDEPENDENT)
    def time_consuming_method(self, x: int) -> int:
        time.sleep(2.0)
        return x * x

Known bugs

  • You 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.

Links

In case you’d like to check my other work or contact me:

Metadata

Release files for omoide-cache 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for omoide-cache 0.1.3
File Size Uploaded
omoide-cache-0.1.3.tar.gz 23.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for omoide-cache 0.1.3
File Interpreter ABI Platform
omoide_cache-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 53.0 kB

Release files / omoide-cache-0.1.3.tar.gz

Download URL omoide-cache-0.1.3.tar.gz
Size 23.4 kB
Tags Source
SHA-256 checksum
How to use checksums
24ed0fc7581b982b7a5d30a673e55abff7876b41459aaa9e6ed8e8fd9473ac64
BLAKE2b-256 checksum
How to use checksums
1ad9f7aae0095603a30df0de63c033b0f9f4ebc626dff61ccd57a0d5fceaf69b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

Release files / omoide_cache-0.1.3-py3-none-any.whl

Download URL omoide_cache-0.1.3-py3-none-any.whl
Size 29.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b686685c08e66667c9b89a7895bee6f5ba42be4023e266a0ea38563fcaa2b4e7
BLAKE2b-256 checksum
How to use checksums
6f960f9c1719a3d11b4f120ab69f2a5de7069394c86fb748fc2b1cd92ff535be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page