GithubHelp home page GithubHelp logo

onecache's Introduction

Coverage Status github status

OneCache

Python cache for sync and async code.

Cache uses LRU algoritm. Cache can optionally have TTL.

Tested in python 3.7, 3.9, 3.11 and pypy3.9 for windows, mac and linux (see github status badge), it should work in versions between them. It may work for python3.6

Usage

from onecache import CacheDecorator
from onecache import AsyncCacheDecorator


class Counter:
    def __init__(self, count=0):
        self.count = count


@pytest.mark.asyncio
async def test_async_cache_counter():
    """Test async cache, counter case."""
    counter = Counter()

    @AsyncCacheDecorator()
    async def mycoro(counter: Counter):
        counter.count += 1
        return counter.count

    assert 1 == (await mycoro(counter))
    assert 1 == (await mycoro(counter))


def test_cache_counter():
    """Test async cache, counter case."""
    counter = Counter()

    @CacheDecorator()
    def sample(counter: Counter):
        counter.count += 1
        return counter.count

    assert 1 == (sample(counter))
    assert 1 == (sample(counter))

Decorator classes supports the following arguments

  • maxsize (int): Maximun number of items to be cached. default: 512
  • ttl (int): time to expire in milliseconds, if None, it does not expire. default: None
  • skip_args (bool): apply cache as the function doesn't have any arguments, default: False
  • cache_class (class): Class to use for cache instance. default: LRUCache
  • refresh_ttl (bool): if cache with ttl, This flag makes key expiration timestamp to be refresh per access. default: False
  • thread_safe (bool): tell decorator to use thread safe lock. default=False
  • max_mem_size (int): max mem size in bytes. Ceil for sum of cache values sizes. default=None which means no limit. For pypy this value is ignored as the objects can change by the JIT compilation.

If num of records exceds maxsize, it drops the oldest.

Development

Install packages with pip-tools:

pip install pip-tools
pip-compile
pip-compile test-requirements.in
pip-sync requirements.txt test-requirements.txt

Contribute

  1. Fork
  2. create a branch feature/your_feature
  3. commit - push - pull request

Thanks :)

onecache's People

Contributors

sonic182 avatar jbonet-df avatar

Watchers

 avatar  avatar

Forkers

horpto jbonet-df

onecache's Issues

Python-3.11 incompatible

Python 3.11 has modified to PyFrameObject C-API for optimization

  Running setup.py install for onecache ... error
  error: subprocess-exited-with-error
  
  × Running setup.py install for onecache did not run successfully.
  │ exit code: 1
  ╰─> [25 lines of output]
      running install
      running build
      running build_py
      creating build
      creating build/lib.macosx-13-arm64-cpython-311
      creating build/lib.macosx-13-arm64-cpython-311/onecache
      copying onecache/__init__.py -> build/lib.macosx-13-arm64-cpython-311/onecache
      copying onecache/utils.py -> build/lib.macosx-13-arm64-cpython-311/onecache
      copying onecache/cache_value.py -> build/lib.macosx-13-arm64-cpython-311/onecache
      running build_ext
      building 'onecache.cache_value' extension
      creating build/temp.macosx-13-arm64-cpython-311
      creating build/temp.macosx-13-arm64-cpython-311/onecache
      clang -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk -I/opt/homebrew/opt/[email protected]/Frameworks/Python.framework/Versions/3.11/include/python3.11 -c onecache/cache_value.c -o build/temp.macosx-13-arm64-cpython-311/onecache/cache_value.o
      onecache/cache_value.c:5187:5: error: incomplete definition of type 'struct _frame'
          __Pyx_PyFrame_SetLineNumber(py_frame, py_line);
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      onecache/cache_value.c:444:62: note: expanded from macro '__Pyx_PyFrame_SetLineNumber'
        #define __Pyx_PyFrame_SetLineNumber(frame, lineno)  (frame)->f_lineno = (lineno)
                                                            ~~~~~~~^
      /opt/homebrew/opt/[email protected]/Frameworks/Python.framework/Versions/3.11/include/python3.11/pytypedefs.h:22:16: note: forward declaration of 'struct _frame'
      typedef struct _frame PyFrameObject;
                     ^
      1 error generated.
      error: command '/usr/bin/clang' failed with exit code 1
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  Rolling back uninstall of onecache
  Moving to /opt/homebrew/lib/python3.11/site-packages/onecache-0.3.1.dist-info/
   from /opt/homebrew/lib/python3.11/site-packages/~necache-0.3.1.dist-info
  Moving to /opt/homebrew/lib/python3.11/site-packages/onecache/
   from /opt/homebrew/lib/python3.11/site-packages/~necache
error: legacy-install-failure

× Encountered error while trying to install package.
╰─> onecache

note: This is an issue with the package mentioned above, not pip.
hint: See above for output from the failure.

I am not great at C otherwise I'd raise a PR for you, but here is how to map call to make it Py3.11 compatible: f_lineno: use PyFrame_GetLineNumber()

ref.: https://bugzilla.redhat.com/show_bug.cgi?id=2093917

onecache==0.4.1 not compatible with pypy

Describe the bug
Library crashing due to onecache incompatibility with PyPy.

To Reproduce
Steps to reproduce the behavior:
pypy -m pip install aiosonic==0.16.0
Run it with PyPy

Expected behavior
aiosonic==0.15.1 is not affected, this is due to onecache dependency bumped to 0.4.1 in aiosonic==0.16.0

Screenshots
N/A

Desktop (please complete the following information):

  • OS: linux/amd64

Additional context

    from aiosonic import HTTPClient
  File "/opt/pypy/lib/pypy3.10/site-packages/aiosonic/__init__.py", line 23, in <module>
    from aiosonic import http_parser
  File "/opt/pypy/lib/pypy3.10/site-packages/aiosonic/http_parser.py", line 6, in <module>
    from aiosonic.connection import Connection
  File "/opt/pypy/lib/pypy3.10/site-packages/aiosonic/connection.py", line 17, in <module>
    from aiosonic.http2 import Http2Handler
  File "/opt/pypy/lib/pypy3.10/site-packages/aiosonic/http2.py", line 10, in <module>
    dlogger = get_debug_logger()
  File "/opt/pypy/lib/pypy3.10/site-packages/onecache/__init__.py", line 211, in wrapper
    self.cache.set(key, resp)
  File "/opt/pypy/lib/pypy3.10/site-packages/onecache/__init__.py", line 44, in set
    key_size = getsizeof(key)
TypeError: getsizeof(...)
    getsizeof(object, default) -> int

    Return the size of object in bytes.

sys.getsizeof(object, default) will always return default on PyPy, and
raise a TypeError if default is not provided.

First note that the CPython documentation says that this function may
raise a TypeError, so if you are seeing it, it means that the program
you are using is not correctly handling this case.

On PyPy, though, it always raises TypeError.  Before looking for
alternatives, please take a moment to read the following explanation as
to why it is the case.  What you are looking for may not be possible.

A memory profiler using this function is most likely to give results
inconsistent with reality on PyPy.  It would be possible to have
sys.getsizeof() return a number (with enough work), but that may or
may not represent how much memory the object uses.  It doesn't even
make really sense to ask how much *one* object uses, in isolation
with the rest of the system.  For example, instances have maps,
which are often shared across many instances; in this case the maps
would probably be ignored by an implementation of sys.getsizeof(),
but their overhead is important in some cases if they are many
instances with unique maps.  Conversely, equal strings may share
their internal string data even if they are different objects---or
empty containers may share parts of their internals as long as they
are empty.  Even stranger, some lists create objects as you read
them; if you try to estimate the size in memory of range(10**6) as
the sum of all items' size, that operation will by itself create one
million integer objects that never existed in the first place.

DeprecationWarning on Py3.12

When running tests on Python 3.12 a deprecation warning appears:

  /usr/local/lib/python3.12/site-packages/onecache/__init__.py:52: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
    expire_at = datetime.utcnow() + timedelta(milliseconds=self.timeout)

I believe it may indirectly affect the aiosonic benchmarks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.