pax_global_header00006660000000000000000000000064141267076160014524gustar00rootroot0000000000000052 comment=ade0f8144c2a17b82a2c1106ef3d564ef1663253 cachelib-0.4.1/000077500000000000000000000000001412670761600132605ustar00rootroot00000000000000cachelib-0.4.1/.editorconfig000066400000000000000000000003311412670761600157320ustar00rootroot00000000000000root = true [*] indent_style = space indent_size = 4 insert_final_newline = true trim_trailing_whitespace = true end_of_line = lf charset = utf-8 max_line_length = 88 [*.{yml,yaml,json,js,css,html}] indent_size = 2 cachelib-0.4.1/.github/000077500000000000000000000000001412670761600146205ustar00rootroot00000000000000cachelib-0.4.1/.github/ISSUE_TEMPLATE/000077500000000000000000000000001412670761600170035ustar00rootroot00000000000000cachelib-0.4.1/.github/ISSUE_TEMPLATE/bug-report.md000066400000000000000000000011531412670761600214130ustar00rootroot00000000000000--- name: Bug report about: Report a bug in CacheLib (not other projects which depend on CacheLib) --- Environment: - Python version: - CacheLib version: cachelib-0.4.1/.github/ISSUE_TEMPLATE/config.yml000066400000000000000000000005271412670761600207770ustar00rootroot00000000000000blank_issues_enabled: false contact_links: - name: Questions url: https://stackoverflow.com/search?tab=relevance&q=cachelib about: Search for and ask questions about your code on Stack Overflow. - name: Questions and discussions url: https://discord.gg/pallets about: Discuss questions about your code on our Discord chat. cachelib-0.4.1/.github/ISSUE_TEMPLATE/feature-request.md000066400000000000000000000006461412670761600224540ustar00rootroot00000000000000--- name: Feature request about: Suggest a new feature for CacheLib --- cachelib-0.4.1/.github/dependabot.yml000066400000000000000000000002351412670761600174500ustar00rootroot00000000000000version: 2 updates: - package-ecosystem: pip directory: "/requirements" schedule: interval: monthly time: "08:00" open-pull-requests-limit: 99 cachelib-0.4.1/.github/pull_request_template.md000066400000000000000000000017131412670761600215630ustar00rootroot00000000000000 - fixes # Checklist: - [ ] Add tests that demonstrate the correct behavior of the change. Tests should fail without the change. - [ ] Add or update relevant docs, in the docs folder and in code. - [ ] Add an entry in `CHANGES.rst` summarizing the change and linking to the issue. - [ ] Add `.. versionchanged::` entries in any relevant code docs. - [ ] Run `pre-commit` hooks and fix any issues. - [ ] Run `pytest` and `tox`, no tests failed. cachelib-0.4.1/.github/workflows/000077500000000000000000000000001412670761600166555ustar00rootroot00000000000000cachelib-0.4.1/.github/workflows/lock.yaml000066400000000000000000000004361412670761600204740ustar00rootroot00000000000000name: 'Lock threads' on: schedule: - cron: '0 0 * * *' jobs: lock: runs-on: ubuntu-latest steps: - uses: dessant/lock-threads@v2 with: github-token: ${{ github.token }} issue-lock-inactive-days: 14 pr-lock-inactive-days: 14 cachelib-0.4.1/.github/workflows/tests.yaml000066400000000000000000000040601412670761600207030ustar00rootroot00000000000000name: Tests on: push: branches: - main - '*.x' paths-ignore: - 'docs/**' - '*.md' - '*.rst' pull_request: branches: - main - '*.x' paths-ignore: - 'docs/**' - '*.md' - '*.rst' jobs: tests: name: ${{ matrix.name }} runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: include: - {name: Linux, python: '3.9', os: ubuntu-latest, tox: py39} - {name: Mac, python: '3.9', os: macos-latest, tox: py39} - {name: '3.8', python: '3.8', os: ubuntu-latest, tox: py38} - {name: '3.7', python: '3.7', os: ubuntu-latest, tox: py37} - {name: '3.6', python: '3.6', os: ubuntu-latest, tox: py36} - {name: Typing, python: '3.9', os: ubuntu-latest, tox: typing} steps: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 with: python-version: ${{ matrix.python }} - name: install external dependencies Mac if: matrix.os == 'macos-latest' run: brew install libmemcached memcached redis - name: install external dependencies Linux if: matrix.os == 'ubuntu-latest' run: | sudo apt-get update sudo apt-get install libmemcached-dev memcached redis-server - name: update pip run: | pip install -U wheel pip install -U setuptools python -m pip install -U pip - name: get pip cache dir id: pip-cache run: echo "::set-output name=dir::$(pip cache dir)" - name: cache pip uses: actions/cache@v2 with: path: ${{ steps.pip-cache.outputs.dir }} key: pip|${{ runner.os }}|${{ matrix.python }}|${{ hashFiles('setup.py') }}|${{ hashFiles('requirements/*.txt') }} - name: cache mypy if: matrix.tox == 'typing' uses: actions/cache@v2 with: path: ./.mypy_cache key: mypy|${{ matrix.python }}|${{ hashFiles('setup.cfg') }} - run: pip install tox - run: tox -e ${{ matrix.tox }} cachelib-0.4.1/.gitignore000066400000000000000000000003471412670761600152540ustar00rootroot00000000000000# general things to ignore build/ dist/ *.egg-info/ *.egg *.eggs *.py[cod] __pycache__/ *.so *~ venv/ env/ .DS_Store *.swp docs/_build # due to using t/nox and pytest .tox .cache .pytest_cache .coverage htmlcov/ .xprocess .vscode cachelib-0.4.1/.pre-commit-config.yaml000066400000000000000000000014321412670761600175410ustar00rootroot00000000000000ci: autoupdate_schedule: monthly repos: - repo: https://github.com/asottile/pyupgrade rev: v2.29.0 hooks: - id: pyupgrade args: ["--py36-plus"] - repo: https://github.com/asottile/reorder_python_imports rev: v2.6.0 hooks: - id: reorder-python-imports args: ["--application-directories", "src"] - repo: https://github.com/psf/black rev: 21.9b0 hooks: - id: black - repo: https://github.com/PyCQA/flake8 rev: 3.9.2 hooks: - id: flake8 additional_dependencies: - flake8-bugbear - flake8-implicit-str-concat - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.0.1 hooks: - id: fix-byte-order-marker - id: trailing-whitespace - id: end-of-file-fixer cachelib-0.4.1/.readthedocs.yaml000066400000000000000000000002331412670761600165050ustar00rootroot00000000000000version: 2 python: install: - requirements: requirements/docs.txt - method: pip path: . sphinx: builder: dirhtml fail_on_warning: true cachelib-0.4.1/CHANGES.rst000066400000000000000000000041111412670761600150570ustar00rootroot00000000000000Version 0.4.1 ------------- Released 2021-10-04 - Fix break in ``RedisCache`` when a host object was passed in ``RedisCache.host`` instead of a string. :pr:`82` Version 0.4.0 ------------- Released 2021-10-03 - All cache types now implement ``BaseCache`` interface both in behavior and method return types. Thus, code written for one cache type should work with any other cache type. :pr:`71` - Add type information for static typing tools. :pr:`48` - ``FileNotFound`` exceptions will not be logged anymore in ``FileSystemCache`` methods in order to avoid polluting application log files. :pr:`69` Version 0.3.0 ------------- Released 2021-08-12 - Optimize ``FileSystemCache`` pruning. :pr:`52` - Fix a bug in ``FileSystemCache`` where entries would not be removed when the total was over the threshold, and the entry count would be lost. :pr:`52` - ``FileSystemCache`` logs system-related exceptions. :pr:`51` - Removal of expired entries in ``FileSystemCache`` is only triggered if the number of entries is over the ``threshhold`` when calling ``set``. ``get`` ``has`` still return ``None`` and ``False`` respectively for expired entries, but will not remove the files. All removals happen at pruning time or explicitly with ``clear`` and ``delete``. :pr:`53` Version 0.2.0 ------------- Released 2021-06-25 - Support for Python 2 has been dropped. Only Python 3.6 and above are supported. - Fix ``FileSystemCache.set`` incorrectly considering value overrides on existing keys as new cache entries. :issue:`18` - ``SimpleCache`` and ``FileSystemCache`` first remove expired entries, followed by older entries, when cleaning up. :pr:`26` - Fix problem where file count was not being updated in ``FileSystemCache.get`` and ``FileSystemCache.has`` after removals. :issue:`20` - When attempting to access non-existent entries with ``Memcached``, these will now be initialized with a given value ``delta``. :pr:`31` Version 0.1.1 ------------- Released 2020-06-20 - Fix ``FileSystemCache`` on Windows. cachelib-0.4.1/CODE_OF_CONDUCT.md000066400000000000000000000064361412670761600160700ustar00rootroot00000000000000# Contributor Covenant Code of Conduct ## Our Pledge In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. ## Our Standards Examples of behavior that contributes to creating a positive environment include: * Using welcoming and inclusive language * Being respectful of differing viewpoints and experiences * Gracefully accepting constructive criticism * Focusing on what is best for the community * Showing empathy towards other community members Examples of unacceptable behavior by participants include: * The use of sexualized language or imagery and unwelcome sexual attention or advances * Trolling, insulting/derogatory comments, and personal or political attacks * Public or private harassment * Publishing others' private information, such as a physical or electronic address, without explicit permission * Other conduct which could reasonably be considered inappropriate in a professional setting ## Our Responsibilities Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. ## Scope This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. ## Enforcement Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at report@palletsprojects.com. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. ## Attribution This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html [homepage]: https://www.contributor-covenant.org For answers to common questions about this code of conduct, see https://www.contributor-covenant.org/faq cachelib-0.4.1/CONTRIBUTING.rst000066400000000000000000000147501412670761600157300ustar00rootroot00000000000000How to contribute to CacheLib ============================= Thank you for considering contributing to CacheLib! Support questions ----------------- Please don't use the issue tracker for this. The issue tracker is a tool to address bugs and feature requests in CacheLib itself. Use one of the following resources for questions about using CacheLib or issues with your own code: - The ``#get-help`` channel on our Discord chat: https://discord.gg/pallets - Ask on `Stack Overflow`_. Search with Google first using: ``site:stackoverflow.com cachelib {search term, exception message, etc.}`` .. _Stack Overflow: https://stackoverflow.com/search?tab=relevance&q=cachelib Reporting issues ---------------- Include the following information in your post: - Describe what you expected to happen. - If possible, include a `minimal reproducible example`_ to help us identify the issue. This also helps check that the issue is not with your own code. - Describe what actually happened. Include the full traceback if there was an exception. - List your Python and CacheLib versions. If possible, check if this issue is already fixed in the latest releases or the latest code in the repository. .. _minimal reproducible example: https://stackoverflow.com/help/minimal-reproducible-example Submitting patches ------------------ If there is not an open issue for what you want to submit, prefer opening one for discussion before working on a PR. You can work on any issue that doesn't have an open PR linked to it or a maintainer assigned to it. These show up in the sidebar. No need to ask if you can work on an issue that interests you. Include the following in your patch: - Use `Black`_ to format your code. This and other tools will run automatically if you install `pre-commit`_ using the instructions below. - Include tests if your patch adds or changes code. Make sure the test fails without your patch. - Update any relevant docs pages and docstrings. Docs pages and docstrings should be wrapped at 72 characters. - Add an entry in ``CHANGES.rst``. Use the same style as other entries. Also include ``.. versionchanged::`` inline changelogs in relevant docstrings. .. _Black: https://black.readthedocs.io .. _pre-commit: https://pre-commit.com First time setup ~~~~~~~~~~~~~~~~ - Download and install the `latest version of git`_. - Configure git with your `username`_ and `email`_. .. code-block:: text $ git config --global user.name 'your name' $ git config --global user.email 'your email' - Make sure you have a `GitHub account`_. - Fork CacheLib to your GitHub account by clicking the `Fork`_ button. - `Clone`_ the main repository locally. .. code-block:: text $ git clone https://github.com/pallets/cachelib $ cd cachelib - Add your fork as a remote to push your work to. Replace ``{username}`` with your username. This names the remote "fork", the default Pallets remote is "origin". .. code-block:: text git remote add fork https://github.com/{username}/cachelib - Create a virtualenv. .. tabs:: .. group-tab:: Linux/macOS .. code-block:: text $ python3 -m venv env $ . env/bin/activate .. group-tab:: Windows .. code-block:: text > py -3 -m venv env > env\Scripts\activate - Upgrade pip and setuptools. .. code-block:: text $ python -m pip install --upgrade pip setuptools - Install the development dependencies, then install CacheLib in editable mode. .. code-block:: text $ pip install -r requirements/dev.txt && pip install -e . - Install the pre-commit hooks. .. code-block:: text $ pre-commit install .. _latest version of git: https://git-scm.com/downloads .. _username: https://docs.github.com/en/github/using-git/setting-your-username-in-git .. _email: https://docs.github.com/en/github/setting-up-and-managing-your-github-user-account/setting-your-commit-email-address .. _GitHub account: https://github.com/join .. _Fork: https://github.com/pallets/cachelib/fork .. _Clone: https://docs.github.com/en/github/getting-started-with-github/fork-a-repo#step-2-create-a-local-clone-of-your-fork Start coding ~~~~~~~~~~~~ - Create a branch to identify the issue you would like to work on. If you're submitting a bug or documentation fix, branch off of the latest ".x" branch. .. code-block:: text $ git fetch origin $ git checkout -b your-branch-name origin/main If you're submitting a feature addition or change, branch off of the "main" branch. .. code-block:: text $ git fetch origin $ git checkout -b your-branch-name origin/main - Using your favorite editor, make your changes, `committing as you go`_. - Include tests that cover any code changes you make. Make sure the test fails without your patch. Run the tests as described below. - Push your commits to your fork on GitHub and `create a pull request`_. Link to the issue being addressed with ``fixes #123`` in the pull request. .. code-block:: text $ git push --set-upstream fork your-branch-name .. _committing as you go: https://dont-be-afraid-to-commit.readthedocs.io/en/latest/git/commandlinegit.html#commit-your-changes .. _create a pull request: https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request Running the tests ~~~~~~~~~~~~~~~~~ Run the basic test suite with pytest. .. code-block:: text $ pytest This runs the tests for the current environment, which is usually sufficient. CI will run the full suite when you submit your pull request. You can run the full test suite with tox if you don't want to wait. .. code-block:: text $ tox Running test coverage ~~~~~~~~~~~~~~~~~~~~~ Generating a report of lines that do not have test coverage can indicate where to start contributing. Run ``pytest`` using ``coverage`` and generate a report. .. code-block:: text $ pip install coverage $ coverage run -m pytest $ coverage html Open ``htmlcov/index.html`` in your browser to explore the report. Read more about `coverage `__. Building the docs ~~~~~~~~~~~~~~~~~ Build the docs in the ``docs`` directory using Sphinx. .. code-block:: text $ cd docs $ make html Open ``_build/html/index.html`` in your browser to view the docs. Read more about `Sphinx `__. cachelib-0.4.1/LICENSE.rst000066400000000000000000000027031412670761600150760ustar00rootroot00000000000000Copyright 2018 Pallets Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. cachelib-0.4.1/MANIFEST.in000066400000000000000000000002331412670761600150140ustar00rootroot00000000000000include CHANGES.rst include tox.ini include requirements/*.txt graft docs prune docs/_build graft tests include src/cachelib/py.typed global-exclude *.pyc cachelib-0.4.1/README.rst000066400000000000000000000016641412670761600147560ustar00rootroot00000000000000CacheLib ======== A collection of cache libraries in the same API interface. Extracted from Werkzeug. Installing ---------- Install and update using `pip`_: .. code-block:: text $ pip install -U cachelib .. _pip: https://pip.pypa.io/en/stable/getting-started/ Donate ------ The Pallets organization develops and supports Flask and the libraries it uses. In order to grow the community of contributors and users, and allow the maintainers to devote more time to the projects, `please donate today`_. .. _please donate today: https://palletsprojects.com/donate Links ----- - Documentation: https://cachelib.readthedocs.io/ - Changes: https://cachelib.readthedocs.io/changes/ - PyPI Releases: https://pypi.org/project/cachelib/ - Source Code: https://github.com/pallets/cachelib/ - Issue Tracker: https://github.com/pallets/cachelib/issues/ - Twitter: https://twitter.com/PalletsTeam - Chat: https://discord.gg/pallets cachelib-0.4.1/docs/000077500000000000000000000000001412670761600142105ustar00rootroot00000000000000cachelib-0.4.1/docs/Makefile000066400000000000000000000011721412670761600156510ustar00rootroot00000000000000# Minimal makefile for Sphinx documentation # # You can set these variables from the command line, and also # from the environment for the first two. SPHINXOPTS ?= SPHINXBUILD ?= sphinx-build SOURCEDIR = . BUILDDIR = _build # Put it first so that "make" without argument is like "make help". help: @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) .PHONY: help Makefile # Catch-all target: route all unknown targets to Sphinx using the new # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). %: Makefile @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) cachelib-0.4.1/docs/base.rst000066400000000000000000000001471412670761600156560ustar00rootroot00000000000000Base API ======== .. automodule:: cachelib.base :members: :undoc-members: :show-inheritance: cachelib-0.4.1/docs/changes.rst000066400000000000000000000000551412670761600163520ustar00rootroot00000000000000Changes ======= .. include:: ../CHANGES.rst cachelib-0.4.1/docs/conf.py000066400000000000000000000032061412670761600155100ustar00rootroot00000000000000from pallets_sphinx_themes import get_version from pallets_sphinx_themes import ProjectLink # Project -------------------------------------------------------------- project = "CacheLib" copyright = "2018 Pallets" author = "Pallets" release, version = get_version("cachelib") # General -------------------------------------------------------------- extensions = [ "sphinx.ext.autodoc", "sphinx.ext.intersphinx", "sphinxcontrib.log_cabinet", "pallets_sphinx_themes", "sphinx_issues", "sphinx_tabs.tabs", ] autodoc_typehints = "description" intersphinx_mapping = { "python": ("https://docs.python.org/3/", None), } issues_github_path = "pallets/cachelib" # HTML ----------------------------------------------------------------- html_theme = "werkzeug" html_theme_options = {"index_sidebar_logo": False} html_context = { "project_links": [ ProjectLink("Donate", "https://palletsprojects.com/donate"), ProjectLink("PyPI Releases", "https://pypi.org/project/cachelib/"), ProjectLink("Source Code", "https://github.com/pallets/cachelib/"), ProjectLink("Issue Tracker", "https://github.com/pallets/cachelib/issues/"), ProjectLink("Twitter", "https://twitter.com/PalletsTeam"), ProjectLink("Chat", "https://discord.gg/pallets"), ] } html_sidebars = { "index": ["project.html", "localtoc.html", "searchbox.html", "ethicalads.html"], "**": ["localtoc.html", "relations.html", "searchbox.html", "ethicalads.html"], } singlehtml_sidebars = {"index": ["project.html", "localtoc.html", "ethicalads.html"]} html_title = f"{project} Documentation ({version})" html_show_sourcelink = False cachelib-0.4.1/docs/file.rst000066400000000000000000000001571412670761600156640ustar00rootroot00000000000000File Backend ============ .. automodule:: cachelib.file :members: :undoc-members: :show-inheritance: cachelib-0.4.1/docs/index.rst000066400000000000000000000003761412670761600160570ustar00rootroot00000000000000CacheLib ======== A collection of cache libraries in the same API interface. Extracted from Werkzeug. .. toctree:: :maxdepth: 2 base simple file redis memcached uwsgi .. toctree:: :maxdepth: 2 license changes cachelib-0.4.1/docs/license.rst000066400000000000000000000001071412670761600163620ustar00rootroot00000000000000BSD-3-Clause License ==================== .. include:: ../LICENSE.rst cachelib-0.4.1/docs/make.bat000066400000000000000000000014331412670761600156160ustar00rootroot00000000000000@ECHO OFF pushd %~dp0 REM Command file for Sphinx documentation if "%SPHINXBUILD%" == "" ( set SPHINXBUILD=sphinx-build ) set SOURCEDIR=. set BUILDDIR=_build if "%1" == "" goto help %SPHINXBUILD% >NUL 2>NUL if errorlevel 9009 ( echo. echo.The 'sphinx-build' command was not found. Make sure you have Sphinx echo.installed, then set the SPHINXBUILD environment variable to point echo.to the full path of the 'sphinx-build' executable. Alternatively you echo.may add the Sphinx directory to PATH. echo. echo.If you don't have Sphinx installed, grab it from echo.http://sphinx-doc.org/ exit /b 1 ) %SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% goto end :help %SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% :end popd cachelib-0.4.1/docs/memcached.rst000066400000000000000000000001761412670761600166540ustar00rootroot00000000000000Memcached Backend ================= .. automodule:: cachelib.memcached :members: :undoc-members: :show-inheritance: cachelib-0.4.1/docs/redis.rst000066400000000000000000000001621412670761600160470ustar00rootroot00000000000000Redis Backend ============= .. automodule:: cachelib.redis :members: :undoc-members: :show-inheritance: cachelib-0.4.1/docs/simple.rst000066400000000000000000000002031412670761600162260ustar00rootroot00000000000000Simple Memory Backend ===================== .. automodule:: cachelib.simple :members: :undoc-members: :show-inheritance: cachelib-0.4.1/docs/uwsgi.rst000066400000000000000000000001621412670761600160770ustar00rootroot00000000000000uWSGI Backend ============= .. automodule:: cachelib.uwsgi :members: :undoc-members: :show-inheritance: cachelib-0.4.1/requirements.txt000066400000000000000000000002641412670761600165460ustar00rootroot00000000000000# To ensure app dependencies are ported from your virtual environment/host machine into your container, run 'pip freeze > requirements.txt' in the terminal to overwrite this file cachelib-0.4.1/requirements/000077500000000000000000000000001412670761600160035ustar00rootroot00000000000000cachelib-0.4.1/requirements/dev.in000066400000000000000000000000751412670761600171130ustar00rootroot00000000000000-r tests.in -r docs.in -r typing.in pip-tools pre-commit tox cachelib-0.4.1/requirements/dev.txt000066400000000000000000000053321412670761600173250ustar00rootroot00000000000000# # This file is autogenerated by pip-compile with python 3.9 # To update, run: # # pip-compile requirements/dev.in # alabaster==0.7.12 # via sphinx appdirs==1.4.4 # via virtualenv attrs==21.2.0 # via pytest babel==2.9.1 # via sphinx certifi==2021.5.30 # via requests cfgv==3.3.0 # via pre-commit chardet==4.0.0 # via requests click==8.0.1 # via pip-tools distlib==0.3.2 # via virtualenv docutils==0.16 # via # sphinx # sphinx-tabs filelock==3.0.12 # via # tox # virtualenv identify==2.2.10 # via pre-commit idna==2.10 # via requests imagesize==1.2.0 # via sphinx iniconfig==1.1.1 # via pytest jinja2==3.0.1 # via sphinx markupsafe==2.0.1 # via jinja2 mypy==0.910 # via -r typing.in mypy-extensions==0.4.3 # via mypy nodeenv==1.6.0 # via pre-commit packaging==20.9 # via # pallets-sphinx-themes # pytest # sphinx # tox pallets-sphinx-themes==2.0.1 # via -r docs.in pep517==0.10.0 # via pip-tools pip-tools==6.3.0 # via -r dev.in pluggy==0.13.1 # via # pytest # tox pre-commit==2.15.0 # via -r dev.in psutil==5.8.0 # via pytest-xprocess py==1.10.0 # via # pytest # tox pygments==2.9.0 # via # sphinx # sphinx-tabs pylibmc==1.6.1 # via -r tests.in pyparsing==2.4.7 # via packaging pytest==6.2.5 # via # -r tests.in # pytest-xprocess pytest-xprocess==0.18.1 # via -r tests.in pytz==2021.1 # via babel pyyaml==5.4.1 # via pre-commit redis==3.5.3 # via -r tests.in requests==2.25.1 # via sphinx six==1.16.0 # via # tox # virtualenv snowballstemmer==2.1.0 # via sphinx sphinx==4.2.0 # via # -r docs.in # pallets-sphinx-themes # sphinx-issues # sphinx-tabs # sphinxcontrib-log-cabinet sphinx-issues==1.2.0 # via -r docs.in sphinx-tabs==3.2.0 # via -r docs.in sphinxcontrib-applehelp==1.0.2 # via sphinx sphinxcontrib-devhelp==1.0.2 # via sphinx sphinxcontrib-htmlhelp==2.0.0 # via sphinx sphinxcontrib-jsmath==1.0.1 # via sphinx sphinxcontrib-log-cabinet==1.0.1 # via -r docs.in sphinxcontrib-qthelp==1.0.3 # via sphinx sphinxcontrib-serializinghtml==1.1.5 # via sphinx toml==0.10.2 # via # mypy # pep517 # pre-commit # pytest # tox tox==3.24.4 # via -r dev.in types-redis==3.5.9 # via -r typing.in typing-extensions==3.10.0.0 # via mypy urllib3==1.26.6 # via requests uwsgi==2.0.19.1 # via -r tests.in virtualenv==20.4.7 # via # pre-commit # tox wheel==0.36.2 # via pip-tools # The following packages are considered to be unsafe in a requirements file: # pip # setuptools cachelib-0.4.1/requirements/docs.in000066400000000000000000000001211412670761600172550ustar00rootroot00000000000000Pallets-Sphinx-Themes Sphinx sphinx-issues sphinxcontrib-log-cabinet sphinx-tabs cachelib-0.4.1/requirements/docs.txt000066400000000000000000000026731412670761600175040ustar00rootroot00000000000000# # This file is autogenerated by pip-compile with python 3.9 # To update, run: # # pip-compile requirements/docs.in # alabaster==0.7.12 # via sphinx babel==2.9.1 # via sphinx certifi==2021.5.30 # via requests chardet==4.0.0 # via requests docutils==0.16 # via # sphinx # sphinx-tabs idna==2.10 # via requests imagesize==1.2.0 # via sphinx jinja2==3.0.1 # via sphinx markupsafe==2.0.1 # via jinja2 packaging==20.9 # via # pallets-sphinx-themes # sphinx pallets-sphinx-themes==2.0.1 # via -r docs.in pygments==2.9.0 # via # sphinx # sphinx-tabs pyparsing==2.4.7 # via packaging pytz==2021.1 # via babel requests==2.25.1 # via sphinx snowballstemmer==2.1.0 # via sphinx sphinx==4.2.0 # via # -r docs.in # pallets-sphinx-themes # sphinx-issues # sphinx-tabs # sphinxcontrib-log-cabinet sphinx-issues==1.2.0 # via -r docs.in sphinx-tabs==3.2.0 # via -r docs.in sphinxcontrib-applehelp==1.0.2 # via sphinx sphinxcontrib-devhelp==1.0.2 # via sphinx sphinxcontrib-htmlhelp==2.0.0 # via sphinx sphinxcontrib-jsmath==1.0.1 # via sphinx sphinxcontrib-log-cabinet==1.0.1 # via -r docs.in sphinxcontrib-qthelp==1.0.3 # via sphinx sphinxcontrib-serializinghtml==1.1.5 # via sphinx urllib3==1.26.6 # via requests # The following packages are considered to be unsafe in a requirements file: # setuptools cachelib-0.4.1/requirements/tests.in000066400000000000000000000000531412670761600174730ustar00rootroot00000000000000pytest pylibmc redis uwsgi pytest-xprocess cachelib-0.4.1/requirements/tests.txt000066400000000000000000000011441412670761600177060ustar00rootroot00000000000000# # This file is autogenerated by pip-compile with python 3.9 # To update, run: # # pip-compile requirements/tests.in # attrs==21.2.0 # via pytest iniconfig==1.1.1 # via pytest packaging==20.9 # via pytest pluggy==0.13.1 # via pytest psutil==5.8.0 # via pytest-xprocess py==1.10.0 # via pytest pylibmc==1.6.1 # via -r tests.in pyparsing==2.4.7 # via packaging pytest==6.2.5 # via # -r tests.in # pytest-xprocess pytest-xprocess==0.18.1 # via -r tests.in redis==3.5.3 # via -r tests.in toml==0.10.2 # via pytest uwsgi==2.0.19.1 # via -r tests.in cachelib-0.4.1/requirements/typing.in000066400000000000000000000000211412670761600176360ustar00rootroot00000000000000mypy types-redis cachelib-0.4.1/requirements/typing.txt000066400000000000000000000004661412670761600200640ustar00rootroot00000000000000# # This file is autogenerated by pip-compile with python 3.9 # To update, run: # # pip-compile requirements/typing.in # mypy==0.910 # via -r typing.in mypy-extensions==0.4.3 # via mypy toml==0.10.2 # via mypy types-redis==3.5.9 # via -r typing.in typing-extensions==3.10.0.0 # via mypy cachelib-0.4.1/setup.cfg000066400000000000000000000042011412670761600150760ustar00rootroot00000000000000[metadata] name = cachelib version = attr: cachelib.__version__ url = https://github.com/pallets/cachelib/ project_urls = Donate = https://palletsprojects.com/donate Documentation = https://cachelib.readthedocs.io/ Changes = https://cachelib.readthedocs.io/changes/ Source Code = https://github.com/pallets/cachelib/ Issue Tracker = https://github.com/pallets/cachelib/issues/ Twitter = https://twitter.com/PalletsTeam Chat = https://discord.gg/pallets license = BSD-3-Clause license_files = LICENSE.rst maintainer = Pallets maintainer_email = contact@palletsprojects.com description = A collection of cache libraries in the same API interface. long_description = file: README.rst long_description_content_type = text/x-rst classifiers = Development Status :: 5 - Production/Stable Intended Audience :: Developers License :: OSI Approved :: BSD License Operating System :: OS Independent Programming Language :: Python [options] packages = find: package_dir = = src include_package_data = true python_requires = >= 3.6 [options.packages.find] where = src [tool:pytest] testpaths = tests filterwarnings = error default::DeprecationWarning:cachelib.uwsgi [coverage:run] branch = True source = cachelib tests [coverage:paths] source = src */site-packages [flake8] # B = bugbear # E = pycodestyle errors # F = flake8 pyflakes # W = pycodestyle warnings # B9 = bugbear opinions # ISC = implicit-str-concat select = B, E, F, W, B9, ISC ignore = # slice notation whitespace, invalid E203 # line length, handled by bugbear B950 E501 # bare except, handled by bugbear B001 E722 # bin op line break, invalid W503 # up to 88 allowed by bugbear B950 max-line-length = 80 [mypy] files = src/cachelib python_version = 3.6 disallow_subclassing_any = True disallow_untyped_calls = True disallow_untyped_defs = True disallow_incomplete_defs = True no_implicit_optional = True local_partial_types = True no_implicit_reexport = True strict_equality = True warn_redundant_casts = True warn_unused_configs = True warn_unused_ignores = True warn_return_any = True warn_unreachable = True cachelib-0.4.1/setup.py000066400000000000000000000000651412670761600147730ustar00rootroot00000000000000from setuptools import setup setup(name="cachelib") cachelib-0.4.1/src/000077500000000000000000000000001412670761600140475ustar00rootroot00000000000000cachelib-0.4.1/src/cachelib/000077500000000000000000000000001412670761600156015ustar00rootroot00000000000000cachelib-0.4.1/src/cachelib/__init__.py000066400000000000000000000006771412670761600177240ustar00rootroot00000000000000from cachelib.base import BaseCache from cachelib.base import NullCache from cachelib.file import FileSystemCache from cachelib.memcached import MemcachedCache from cachelib.redis import RedisCache from cachelib.simple import SimpleCache from cachelib.uwsgi import UWSGICache __all__ = [ "BaseCache", "NullCache", "SimpleCache", "FileSystemCache", "MemcachedCache", "RedisCache", "UWSGICache", ] __version__ = "0.4.1" cachelib-0.4.1/src/cachelib/base.py000066400000000000000000000150711412670761600170710ustar00rootroot00000000000000import typing as _t class BaseCache: """Baseclass for the cache systems. All the cache systems implement this API or a superset of it. :param default_timeout: the default timeout (in seconds) that is used if no timeout is specified on :meth:`set`. A timeout of 0 indicates that the cache never expires. """ def __init__(self, default_timeout: int = 300): self.default_timeout = default_timeout def _normalize_timeout(self, timeout: _t.Optional[int]) -> int: if timeout is None: timeout = self.default_timeout return timeout def get(self, key: str) -> _t.Any: """Look up key in the cache and return the value for it. :param key: the key to be looked up. :returns: The value if it exists and is readable, else ``None``. """ return None def delete(self, key: str) -> bool: """Delete `key` from the cache. :param key: the key to delete. :returns: Whether the key existed and has been deleted. :rtype: boolean """ return True def get_many(self, *keys: str) -> _t.List[_t.Any]: """Returns a list of values for the given keys. For each key an item in the list is created:: foo, bar = cache.get_many("foo", "bar") Has the same error handling as :meth:`get`. :param keys: The function accepts multiple keys as positional arguments. """ return [self.get(k) for k in keys] def get_dict(self, *keys: str) -> _t.Dict[str, _t.Any]: """Like :meth:`get_many` but return a dict:: d = cache.get_dict("foo", "bar") foo = d["foo"] bar = d["bar"] :param keys: The function accepts multiple keys as positional arguments. """ return dict(zip(keys, self.get_many(*keys))) def set( self, key: str, value: _t.Any, timeout: _t.Optional[int] = None ) -> _t.Optional[bool]: """Add a new key/value to the cache (overwrites value, if key already exists in the cache). :param key: the key to set :param value: the value for the key :param timeout: the cache timeout for the key in seconds (if not specified, it uses the default timeout). A timeout of 0 indicates that the cache never expires. :returns: ``True`` if key has been updated, ``False`` for backend errors. Pickling errors, however, will raise a subclass of ``pickle.PickleError``. :rtype: boolean """ return True def add(self, key: str, value: _t.Any, timeout: _t.Optional[int] = None) -> bool: """Works like :meth:`set` but does not overwrite the values of already existing keys. :param key: the key to set :param value: the value for the key :param timeout: the cache timeout for the key in seconds (if not specified, it uses the default timeout). A timeout of 0 indicates that the cache never expires. :returns: Same as :meth:`set`, but also ``False`` for already existing keys. :rtype: boolean """ return True def set_many( self, mapping: _t.Dict[str, _t.Any], timeout: _t.Optional[int] = None ) -> _t.List[_t.Any]: """Sets multiple keys and values from a mapping. :param mapping: a mapping with the keys/values to set. :param timeout: the cache timeout for the key in seconds (if not specified, it uses the default timeout). A timeout of 0 indicates that the cache never expires. :returns: A list containing all keys sucessfuly set :rtype: boolean """ set_keys = [] for key, value in mapping.items(): if self.set(key, value, timeout): set_keys.append(key) return set_keys def delete_many(self, *keys: str) -> _t.List[_t.Any]: """Deletes multiple keys at once. :param keys: The function accepts multiple keys as positional arguments. :returns: A list containing all sucessfuly deleted keys :rtype: boolean """ deleted_keys = [] for key in keys: if self.delete(key): deleted_keys.append(key) return deleted_keys def has(self, key: str) -> bool: """Checks if a key exists in the cache without returning it. This is a cheap operation that bypasses loading the actual data on the backend. :param key: the key to check """ raise NotImplementedError( "%s doesn't have an efficient implementation of `has`. That " "means it is impossible to check whether a key exists without " "fully loading the key's data. Consider using `self.get` " "explicitly if you don't care about performance." ) def clear(self) -> bool: """Clears the cache. Keep in mind that not all caches support completely clearing the cache. :returns: Whether the cache has been cleared. :rtype: boolean """ return True def inc(self, key: str, delta: int = 1) -> _t.Optional[int]: """Increments the value of a key by `delta`. If the key does not yet exist it is initialized with `delta`. For supporting caches this is an atomic operation. :param key: the key to increment. :param delta: the delta to add. :returns: The new value or ``None`` for backend errors. """ value = (self.get(key) or 0) + delta return value if self.set(key, value) else None def dec(self, key: str, delta: int = 1) -> _t.Optional[int]: """Decrements the value of a key by `delta`. If the key does not yet exist it is initialized with `-delta`. For supporting caches this is an atomic operation. :param key: the key to increment. :param delta: the delta to subtract. :returns: The new value or `None` for backend errors. """ value = (self.get(key) or 0) - delta return value if self.set(key, value) else None class NullCache(BaseCache): """A cache that doesn't cache. This can be useful for unit testing. :param default_timeout: a dummy parameter that is ignored but exists for API compatibility with other caches. """ def has(self, key: str) -> bool: return False cachelib-0.4.1/src/cachelib/file.py000066400000000000000000000224211412670761600170730ustar00rootroot00000000000000import errno import logging import os import pickle import tempfile import typing as _t from hashlib import md5 from pathlib import Path from time import time from cachelib.base import BaseCache class FileSystemCache(BaseCache): """A cache that stores the items on the file system. This cache depends on being the only user of the `cache_dir`. Make absolutely sure that nobody but this cache stores files there or otherwise the cache will randomly delete files therein. :param cache_dir: the directory where cache files are stored. :param threshold: the maximum number of items the cache stores before it starts deleting some. A threshold value of 0 indicates no threshold. :param default_timeout: the default timeout that is used if no timeout is specified on :meth:`~BaseCache.set`. A timeout of 0 indicates that the cache never expires. :param mode: the file mode wanted for the cache files, default 0600 """ #: used for temporary files by the FileSystemCache _fs_transaction_suffix = ".__wz_cache" #: keep amount of files in a cache element _fs_count_file = "__wz_cache_count" def __init__( self, cache_dir: str, threshold: int = 500, default_timeout: int = 300, mode: int = 0o600, ): BaseCache.__init__(self, default_timeout) self._path = cache_dir self._threshold = threshold self._mode = mode try: os.makedirs(self._path) except OSError as ex: if ex.errno != errno.EEXIST: raise # If there are many files and a zero threshold, # the list_dir can slow initialisation massively if self._threshold != 0: self._update_count(value=len(list(self._list_dir()))) @property def _file_count(self) -> int: return self.get(self._fs_count_file) or 0 def _update_count( self, delta: _t.Optional[int] = None, value: _t.Optional[int] = None ) -> None: # If we have no threshold, don't count files if self._threshold == 0: return if delta: new_count = self._file_count + delta else: new_count = value or 0 self.set(self._fs_count_file, new_count, mgmt_element=True) def _normalize_timeout(self, timeout: _t.Optional[int]) -> int: timeout = BaseCache._normalize_timeout(self, timeout) if timeout != 0: timeout = int(time()) + timeout return int(timeout) def _is_mgmt(self, name: str) -> bool: fshash = self._get_filename(self._fs_count_file).split(os.sep)[-1] return name == fshash or name.endswith(self._fs_transaction_suffix) def _list_dir(self) -> _t.Generator[str, None, None]: """return a list of (fully qualified) cache filenames""" return ( os.path.join(self._path, fn) for fn in os.listdir(self._path) if not self._is_mgmt(fn) ) def _over_threshold(self) -> bool: return self._threshold != 0 and self._file_count > self._threshold def _remove_expired(self, now: float) -> None: for fname in self._list_dir(): try: with open(fname, "rb") as f: expires = pickle.load(f) if expires != 0 and expires < now: os.remove(fname) self._update_count(delta=-1) except FileNotFoundError: pass except (OSError, EOFError): logging.warning( "Exception raised while handling cache file '%s'", fname, exc_info=True, ) def _remove_older(self) -> bool: exp_fname_tuples = [] for fname in self._list_dir(): try: with open(fname, "rb") as f: exp_fname_tuples.append((pickle.load(f), fname)) except FileNotFoundError: pass except (OSError, EOFError): logging.warning( "Exception raised while handling cache file '%s'", fname, exc_info=True, ) fname_sorted = ( fname for _, fname in sorted( exp_fname_tuples, key=lambda item: item[0] # type: ignore ) ) for fname in fname_sorted: try: os.remove(fname) self._update_count(delta=-1) except FileNotFoundError: pass except OSError: logging.warning( "Exception raised while handling cache file '%s'", fname, exc_info=True, ) return False if not self._over_threshold(): break return True def _prune(self) -> None: if self._over_threshold(): now = time() self._remove_expired(now) # if still over threshold if self._over_threshold(): self._remove_older() def clear(self) -> bool: for i, fname in enumerate(self._list_dir()): try: os.remove(fname) except FileNotFoundError: pass except OSError: logging.warning( "Exception raised while handling cache file '%s'", fname, exc_info=True, ) self._update_count(delta=-i) return False self._update_count(value=0) return True def _get_filename(self, key: str) -> str: if isinstance(key, str): bkey = key.encode("utf-8") # XXX unicode review bkey_hash = md5(bkey).hexdigest() return os.path.join(self._path, bkey_hash) def get(self, key: str) -> _t.Any: filename = self._get_filename(key) try: with open(filename, "rb") as f: pickle_time = pickle.load(f) if pickle_time == 0 or pickle_time >= time(): return pickle.load(f) except FileNotFoundError: pass except (OSError, EOFError, pickle.PickleError): logging.warning( "Exception raised while handling cache file '%s'", filename, exc_info=True, ) return None def add(self, key: str, value: _t.Any, timeout: _t.Optional[int] = None) -> bool: filename = self._get_filename(key) if not os.path.exists(filename): return self.set(key, value, timeout) return False def set( self, key: str, value: _t.Any, timeout: _t.Optional[int] = None, mgmt_element: bool = False, ) -> bool: # Management elements have no timeout if mgmt_element: timeout = 0 # Don't prune on management element update, to avoid loop else: self._prune() timeout = self._normalize_timeout(timeout) filename = self._get_filename(key) overwrite = os.path.isfile(filename) try: fd, tmp = tempfile.mkstemp( suffix=self._fs_transaction_suffix, dir=self._path ) with os.fdopen(fd, "wb") as f: pickle.dump(timeout, f, 1) pickle.dump(value, f, pickle.HIGHEST_PROTOCOL) os.replace(tmp, filename) os.chmod(filename, self._mode) fsize = Path(filename).stat().st_size except OSError: logging.warning( "Exception raised while handling cache file '%s'", filename, exc_info=True, ) return False else: # Management elements should not count towards threshold if not overwrite and not mgmt_element: self._update_count(delta=1) return fsize > 0 # function should fail if file is empty def delete(self, key: str, mgmt_element: bool = False) -> bool: try: os.remove(self._get_filename(key)) except FileNotFoundError: # if file doesn't exist we consider it deleted return True except OSError: logging.warning("Exception raised while handling cache file", exc_info=True) return False else: # Management elements should not count towards threshold if not mgmt_element: self._update_count(delta=-1) return True def has(self, key: str) -> bool: filename = self._get_filename(key) try: with open(filename, "rb") as f: pickle_time = pickle.load(f) if pickle_time == 0 or pickle_time >= time(): return True else: return False except FileNotFoundError: # if there is no file there is no key return False except (OSError, EOFError, pickle.PickleError): logging.warning( "Exception raised while handling cache file '%s'", filename, exc_info=True, ) return False cachelib-0.4.1/src/cachelib/memcached.py000066400000000000000000000157531412670761600200740ustar00rootroot00000000000000import re import typing as _t from time import time from cachelib.base import BaseCache _test_memcached_key = re.compile(r"[^\x00-\x21\xff]{1,250}$").match class MemcachedCache(BaseCache): """A cache that uses memcached as backend. The first argument can either be an object that resembles the API of a :class:`memcache.Client` or a tuple/list of server addresses. In the event that a tuple/list is passed, Werkzeug tries to import the best available memcache library. This cache looks into the following packages/modules to find bindings for memcached: - ``pylibmc`` - ``google.appengine.api.memcached`` - ``memcached`` - ``libmc`` Implementation notes: This cache backend works around some limitations in memcached to simplify the interface. For example unicode keys are encoded to utf-8 on the fly. Methods such as :meth:`~BaseCache.get_dict` return the keys in the same format as passed. Furthermore all get methods silently ignore key errors to not cause problems when untrusted user data is passed to the get methods which is often the case in web applications. :param servers: a list or tuple of server addresses or alternatively a :class:`memcache.Client` or a compatible client. :param default_timeout: the default timeout that is used if no timeout is specified on :meth:`~BaseCache.set`. A timeout of 0 indicates that the cache never expires. :param key_prefix: a prefix that is added before all keys. This makes it possible to use the same memcached server for different applications. Keep in mind that :meth:`~BaseCache.clear` will also clear keys with a different prefix. """ def __init__( self, servers: _t.Any = None, default_timeout: int = 300, key_prefix: _t.Optional[str] = None, ): BaseCache.__init__(self, default_timeout) if servers is None or isinstance(servers, (list, tuple)): if servers is None: servers = ["127.0.0.1:11211"] self._client = self.import_preferred_memcache_lib(servers) if self._client is None: raise RuntimeError("no memcache module found") else: # NOTE: servers is actually an already initialized memcache # client. self._client = servers self.key_prefix = key_prefix def _normalize_key(self, key: str) -> str: if self.key_prefix: key = self.key_prefix + key return key def _normalize_timeout(self, timeout: _t.Optional[int]) -> int: timeout = BaseCache._normalize_timeout(self, timeout) if timeout > 0: timeout = int(time()) + timeout return timeout def get(self, key: str) -> _t.Any: key = self._normalize_key(key) # memcached doesn't support keys longer than that. Because often # checks for so long keys can occur because it's tested from user # submitted data etc we fail silently for getting. if _test_memcached_key(key): return self._client.get(key) def get_dict(self, *keys: str) -> _t.Dict[str, _t.Any]: key_mapping = {} for key in keys: encoded_key = self._normalize_key(key) if _test_memcached_key(key): key_mapping[encoded_key] = key _keys = list(key_mapping) d = rv = self._client.get_multi(_keys) # type: _t.Dict[str, _t.Any] if self.key_prefix: rv = {} for key, value in d.items(): rv[key_mapping[key]] = value if len(rv) < len(keys): for key in keys: if key not in rv: rv[key] = None return rv def add(self, key: str, value: _t.Any, timeout: _t.Optional[int] = None) -> bool: key = self._normalize_key(key) timeout = self._normalize_timeout(timeout) return bool(self._client.add(key, value, timeout)) def set( self, key: str, value: _t.Any, timeout: _t.Optional[int] = None ) -> _t.Optional[bool]: key = self._normalize_key(key) timeout = self._normalize_timeout(timeout) return bool(self._client.set(key, value, timeout)) def get_many(self, *keys: str) -> _t.List[_t.Any]: d = self.get_dict(*keys) return [d[key] for key in keys] def set_many( self, mapping: _t.Dict[str, _t.Any], timeout: _t.Optional[int] = None ) -> _t.List[_t.Any]: new_mapping = {} for key, value in mapping.items(): key = self._normalize_key(key) new_mapping[key] = value timeout = self._normalize_timeout(timeout) failed_keys = self._client.set_multi( new_mapping, timeout ) # type: _t.List[_t.Any] k_normkey = zip(mapping.keys(), new_mapping.keys()) return [k for k, nkey in k_normkey if nkey not in failed_keys] def delete(self, key: str) -> bool: key = self._normalize_key(key) if _test_memcached_key(key): return bool(self._client.delete(key)) return False def delete_many(self, *keys: str) -> _t.List[_t.Any]: new_keys = [] for key in keys: key = self._normalize_key(key) if _test_memcached_key(key): new_keys.append(key) self._client.delete_multi(new_keys) return [k for k in new_keys if not self.has(k)] def has(self, key: str) -> bool: key = self._normalize_key(key) if _test_memcached_key(key): return bool(self._client.append(key, "")) return False def clear(self) -> bool: return bool(self._client.flush_all()) def inc(self, key: str, delta: int = 1) -> _t.Optional[int]: key = self._normalize_key(key) value = (self._client.get(key) or 0) + delta return value if self.set(key, value) else None def dec(self, key: str, delta: int = 1) -> _t.Optional[int]: key = self._normalize_key(key) value = (self._client.get(key) or 0) - delta return value if self.set(key, value) else None def import_preferred_memcache_lib(self, servers: _t.Any) -> _t.Any: """Returns an initialized memcache client. Used by the constructor.""" try: import pylibmc # type: ignore except ImportError: pass else: return pylibmc.Client(servers) try: from google.appengine.api import memcache # type: ignore except ImportError: pass else: return memcache.Client() try: import memcache # type: ignore except ImportError: pass else: return memcache.Client(servers) try: import libmc # type: ignore except ImportError: pass else: return libmc.Client(servers) cachelib-0.4.1/src/cachelib/py.typed000066400000000000000000000000001412670761600172660ustar00rootroot00000000000000cachelib-0.4.1/src/cachelib/redis.py000066400000000000000000000142271412670761600172670ustar00rootroot00000000000000import pickle import typing as _t from cachelib.base import BaseCache class RedisCache(BaseCache): """Uses the Redis key-value store as a cache backend. The first argument can be either a string denoting address of the Redis server or an object resembling an instance of a redis.Redis class. Note: Python Redis API already takes care of encoding unicode strings on the fly. :param host: address of the Redis server or an object which API is compatible with the official Python Redis client (redis-py). :param port: port number on which Redis server listens for connections. :param password: password authentication for the Redis server. :param db: db (zero-based numeric index) on Redis Server to connect. :param default_timeout: the default timeout that is used if no timeout is specified on :meth:`~BaseCache.set`. A timeout of 0 indicates that the cache never expires. :param key_prefix: A prefix that should be added to all keys. Any additional keyword arguments will be passed to ``redis.Redis``. """ def __init__( self, host: _t.Any = "localhost", port: int = 6379, password: _t.Optional[str] = None, db: int = 0, default_timeout: int = 300, key_prefix: _t.Optional[str] = None, **kwargs: _t.Any ): BaseCache.__init__(self, default_timeout) if host is None: raise ValueError("RedisCache host parameter may not be None") if isinstance(host, str): try: import redis except ImportError as err: raise RuntimeError("no redis module found") from err if kwargs.get("decode_responses", None): raise ValueError("decode_responses is not supported by RedisCache.") self._client = redis.Redis( host=host, port=port, password=password, db=db, **kwargs ) else: self._client = host self.key_prefix = key_prefix or "" def _normalize_timeout(self, timeout: _t.Optional[int]) -> int: timeout = BaseCache._normalize_timeout(self, timeout) if timeout == 0: timeout = -1 return timeout def dump_object(self, value: _t.Any) -> bytes: """Dumps an object into a string for redis. By default it serializes integers as regular string and pickle dumps everything else. """ if isinstance(type(value), int): return str(value).encode("ascii") return b"!" + pickle.dumps(value) def load_object(self, value: _t.Optional[bytes]) -> _t.Any: """The reversal of :meth:`dump_object`. This might be called with None. """ if value is None: return None if value.startswith(b"!"): try: return pickle.loads(value[1:]) except pickle.PickleError: return None try: return int(value) except ValueError: # before 0.8 we did not have serialization. Still support that. return value def get(self, key: str) -> _t.Any: return self.load_object(self._client.get(self.key_prefix + key)) def get_many(self, *keys: str) -> _t.List[_t.Any]: if self.key_prefix: prefixed_keys = [self.key_prefix + key for key in keys] else: prefixed_keys = [k for k in keys] return [self.load_object(x) for x in self._client.mget(prefixed_keys)] def set( self, key: str, value: _t.Any, timeout: _t.Optional[int] = None ) -> _t.Optional[bool]: timeout = self._normalize_timeout(timeout) dump = self.dump_object(value) if timeout == -1: result = self._client.set(name=self.key_prefix + key, value=dump) else: result = self._client.setex( name=self.key_prefix + key, value=dump, time=timeout ) return result def add(self, key: str, value: _t.Any, timeout: _t.Optional[int] = None) -> bool: timeout = self._normalize_timeout(timeout) dump = self.dump_object(value) return self._client.setnx( name=self.key_prefix + key, value=dump ) and self._client.expire(name=self.key_prefix + key, time=timeout) def set_many( self, mapping: _t.Dict[str, _t.Any], timeout: _t.Optional[int] = None ) -> _t.List[_t.Any]: timeout = self._normalize_timeout(timeout) # Use transaction=False to batch without calling redis MULTI # which is not supported by twemproxy pipe = self._client.pipeline(transaction=False) for key, value in mapping.items(): dump = self.dump_object(value) if timeout == -1: pipe.set(name=self.key_prefix + key, value=dump) else: pipe.setex(name=self.key_prefix + key, value=dump, time=timeout) results = pipe.execute() return [k for k, was_set in zip(mapping.keys(), results) if was_set] def delete(self, key: str) -> bool: return bool(self._client.delete(self.key_prefix + key)) def delete_many(self, *keys: str) -> _t.List[_t.Any]: if not keys: return [] if self.key_prefix: prefixed_keys = [self.key_prefix + key for key in keys] else: prefixed_keys = [k for k in keys] self._client.delete(*prefixed_keys) return [k for k in prefixed_keys if not self.has(k)] def has(self, key: str) -> bool: return bool(self._client.exists(self.key_prefix + key)) def clear(self) -> bool: status = 0 if self.key_prefix: keys = self._client.keys(self.key_prefix + "*") if keys: status = self._client.delete(*keys) else: status = self._client.flushdb() return bool(status) def inc(self, key: str, delta: int = 1) -> _t.Optional[int]: return self._client.incr(name=self.key_prefix + key, amount=delta) def dec(self, key: str, delta: int = 1) -> _t.Optional[int]: return self._client.incr(name=self.key_prefix + key, amount=-delta) cachelib-0.4.1/src/cachelib/simple.py000066400000000000000000000065361412670761600174560ustar00rootroot00000000000000import pickle import typing as _t from time import time from cachelib.base import BaseCache class SimpleCache(BaseCache): """Simple memory cache for single process environments. This class exists mainly for the development server and is not 100% thread safe. It tries to use as many atomic operations as possible and no locks for simplicity but it could happen under heavy load that keys are added multiple times. :param threshold: the maximum number of items the cache stores before it starts deleting some. :param default_timeout: the default timeout that is used if no timeout is specified on :meth:`~BaseCache.set`. A timeout of 0 indicates that the cache never expires. """ def __init__(self, threshold: int = 500, default_timeout: int = 300): BaseCache.__init__(self, default_timeout) self._cache: _t.Dict[str, _t.Any] = {} self._threshold = threshold or 500 # threshold = 0 def _over_threshold(self) -> bool: return len(self._cache) > self._threshold def _remove_expired(self, now: float) -> None: toremove = [k for k, (expires, _) in self._cache.items() if expires < now] for k in toremove: self._cache.pop(k, None) def _remove_older(self) -> None: k_ordered = ( k for k, v in sorted( self._cache.items(), key=lambda item: item[1][0] # type: ignore ) ) for k in k_ordered: self._cache.pop(k, None) if not self._over_threshold(): break def _prune(self) -> None: if self._over_threshold(): now = time() self._remove_expired(now) # remove older items if still over threshold if self._over_threshold(): self._remove_older() def _normalize_timeout(self, timeout: _t.Optional[int]) -> int: timeout = BaseCache._normalize_timeout(self, timeout) if timeout > 0: timeout = int(time()) + timeout return timeout def get(self, key: str) -> _t.Any: try: expires, value = self._cache[key] if expires == 0 or expires > time(): return pickle.loads(value) except (KeyError, pickle.PickleError): return None def set( self, key: str, value: _t.Any, timeout: _t.Optional[int] = None ) -> _t.Optional[bool]: expires = self._normalize_timeout(timeout) self._prune() self._cache[key] = (expires, pickle.dumps(value, pickle.HIGHEST_PROTOCOL)) return True def add(self, key: str, value: _t.Any, timeout: _t.Optional[int] = None) -> bool: expires = self._normalize_timeout(timeout) self._prune() item = (expires, pickle.dumps(value, pickle.HIGHEST_PROTOCOL)) if key in self._cache: return False self._cache.setdefault(key, item) return True def delete(self, key: str) -> bool: return self._cache.pop(key, None) is not None def has(self, key: str) -> bool: try: expires, value = self._cache[key] return bool(expires == 0 or expires > time()) except KeyError: return False def clear(self) -> bool: self._cache.clear() return not bool(self._cache) cachelib-0.4.1/src/cachelib/uwsgi.py000066400000000000000000000044541412670761600173200ustar00rootroot00000000000000import pickle import platform import typing as _t from cachelib.base import BaseCache class UWSGICache(BaseCache): """Implements the cache using uWSGI's caching framework. .. note:: This class cannot be used when running under PyPy, because the uWSGI API implementation for PyPy is lacking the needed functionality. :param default_timeout: The default timeout in seconds. :param cache: The name of the caching instance to connect to, for example: mycache@localhost:3031, defaults to an empty string, which means uWSGI will cache in the local instance. If the cache is in the same instance as the werkzeug app, you only have to provide the name of the cache. """ def __init__(self, default_timeout: int = 300, cache: str = ""): BaseCache.__init__(self, default_timeout) if platform.python_implementation() == "PyPy": raise RuntimeError( "uWSGI caching does not work under PyPy, see " "the docs for more details." ) try: import uwsgi # type: ignore self._uwsgi = uwsgi except ImportError as err: raise RuntimeError( "uWSGI could not be imported, are you running under uWSGI?" ) from err self.cache = cache def get(self, key: str) -> _t.Any: rv = self._uwsgi.cache_get(key, self.cache) if rv is None: return return pickle.loads(rv) def delete(self, key: str) -> bool: return bool(self._uwsgi.cache_del(key, self.cache)) def set( self, key: str, value: _t.Any, timeout: _t.Optional[int] = None ) -> _t.Optional[bool]: result = self._uwsgi.cache_update( key, pickle.dumps(value), self._normalize_timeout(timeout), self.cache ) # type: bool return result def add(self, key: str, value: _t.Any, timeout: _t.Optional[int] = None) -> bool: return bool( self._uwsgi.cache_set( key, pickle.dumps(value), self._normalize_timeout(timeout), self.cache ) ) def clear(self) -> bool: return bool(self._uwsgi.cache_clear(self.cache)) def has(self, key: str) -> bool: return self._uwsgi.cache_exists(key, self.cache) is not None cachelib-0.4.1/tests/000077500000000000000000000000001412670761600144225ustar00rootroot00000000000000cachelib-0.4.1/tests/clear.py000066400000000000000000000005141412670761600160620ustar00rootroot00000000000000from conftest import TestData class ClearTests(TestData): """Tests for the optional 'clear' method specified by BaseCache""" def test_clear(self): cache = self.cache_factory() assert cache.set_many(self.sample_pairs) assert cache.clear() assert not any(cache.get_many(*self.sample_pairs)) cachelib-0.4.1/tests/common.py000066400000000000000000000054261412670761600162730ustar00rootroot00000000000000from time import sleep import pytest from conftest import TestData from conftest import under_uwsgi class CommonTests(TestData): """A base set of tests to be run for all cache types""" def test_set_get(self): cache = self.cache_factory() for k, v in self.sample_pairs.items(): assert cache.set(k, v) assert cache.get(k) == v def test_set_get_many(self): cache = self.cache_factory() result = cache.set_many(self.sample_pairs) assert result == list(self.sample_pairs.keys()) values = cache.get_many(*self.sample_pairs) assert values == list(self.sample_pairs.values()) def test_get_dict(self): cache = self.cache_factory() cache.set_many(self.sample_pairs) d = cache.get_dict(*self.sample_pairs) assert d == self.sample_pairs def test_delete(self): cache = self.cache_factory() for k, v in self.sample_pairs.items(): cache.set(k, v) assert cache.delete(k) assert not cache.get(k) def test_delete_many(self): cache = self.cache_factory() cache.set_many(self.sample_pairs) result = cache.delete_many(*self.sample_pairs) assert result == list(self.sample_pairs.keys()) assert not any(cache.get_many(*self.sample_pairs)) def test_add(self): cache = self.cache_factory() cache.set_many(self.sample_pairs) for k in self.sample_pairs: assert not cache.add(k, "updated") assert cache.get_many(*self.sample_pairs) == list(self.sample_pairs.values()) for k, v in self.sample_pairs.items(): assert cache.add(f"{k}-new", v) assert cache.get(f"{k}-new") == v def test_inc_dec(self): cache = self.cache_factory() for n in self.sample_numbers: assert not cache.get(f"{n}-key-inc") assert cache.inc(f"{n}-key-inc", n) == n assert cache.get(f"{n}-key-inc") == n assert cache.dec(f"{n}-key-dec", n) == -n assert cache.get(f"{n}-key-dec") == -n assert cache.dec(f"{n}-key-inc", 5) == n - 5 def test_expiration(self): if under_uwsgi(): pytest.skip( "uwsgi uses a separate sweeper thread to clean" " expired chache entries, thus the testing" " of such feature must be handled differently" " from other cache types." ) cache = self.cache_factory() for k, v in self.sample_pairs.items(): cache.set(f"{k}-t0", v, timeout=0) cache.set(f"{k}-t1", v, timeout=1) sleep(4) for k, v in self.sample_pairs.items(): assert cache.get(f"{k}-t0") == v assert not cache.get(f"{k}-t1") cachelib-0.4.1/tests/conftest.py000066400000000000000000000041531412670761600166240ustar00rootroot00000000000000import os import warnings from pathlib import Path import pytest from xprocess import ProcessStarter def under_uwsgi(): try: import uwsgi # noqa: F401 except ImportError: return False else: return True @pytest.hookimpl() def pytest_sessionfinish(session, exitstatus): if under_uwsgi(): try: script_path = Path(os.environ["TMPDIR"], "return_pytest_exit_code.py") except KeyError: warnings.warn( "Pytest could not find tox 'TMPDIR' in the environment," " make sure the variable is set in the project tox.ini" " file if you are running under tox." ) else: with open(script_path, mode="w") as f: f.write(f"import sys; sys.exit({exitstatus})") @pytest.fixture(scope="class") def redis_server(xprocess): package_name = "redis" pytest.importorskip( modname=package_name, reason=f"could not find python package {package_name}" ) class Starter(ProcessStarter): pattern = "[Rr]eady to accept connections" args = ["redis-server", "--port 6360"] xprocess.ensure(package_name, Starter) yield xprocess.getinfo(package_name).terminate() @pytest.fixture(scope="class") def memcached_server(xprocess): package_name = "pylibmc" pytest.importorskip( modname=package_name, reason=f"could not find python package {package_name}" ) class Starter(ProcessStarter): pattern = "server listening" args = ["memcached", "-vv"] xprocess.ensure(package_name, Starter) yield xprocess.getinfo(package_name).terminate() class TestData: """This class centralizes all data samples used in tests""" sample_numbers = [0, 10, 1024000, 9, 5000000000000, 99, 738, 2000000] sample_pairs = { "128": False, "beef": True, "crevettes": {}, "1024": "spam", "bacon": "eggs", "sausage": 2048, "3072": [], "brandy": [{}, "fried eggs"], "lobster": ["baked beans", [512]], "4096": {"sauce": [], 256: "truffle"}, } cachelib-0.4.1/tests/has.py000066400000000000000000000005321412670761600155470ustar00rootroot00000000000000from conftest import TestData class HasTests(TestData): """Tests for the optional 'has' method specified by BaseCache""" def test_has(self): cache = self.cache_factory() assert cache.set_many(self.sample_pairs) for k in self.sample_pairs: assert cache.has(k) assert not cache.has("unknown") cachelib-0.4.1/tests/test_base_cache.py000066400000000000000000000035361412670761600200770ustar00rootroot00000000000000import pytest from cachelib import BaseCache @pytest.fixture(autouse=True) def cache_factory(request): def _factory(self, *args, **kwargs): return BaseCache(*args, **kwargs) request.cls.cache_factory = _factory class TestBaseCache: def test_get(self): cache = self.cache_factory() assert cache.get("bacon") is None def test_delete(self): cache = self.cache_factory() assert cache.delete("eggs") def test_get_many(self): cache = self.cache_factory() keys = ["bacon", "spam", "eggs"] expected = [None] * 3 assert cache.get_many(*keys) == expected def test_get_dict(self): cache = self.cache_factory() keys = ["bacon", "spam", "eggs"] expected = dict.fromkeys(keys, None) assert cache.get_dict(*keys) == expected def test_set(self): cache = self.cache_factory() assert cache.set("sausage", "tomato") def test_add(self): cache = self.cache_factory() assert cache.add("baked", "beans") def test_set_many(self): cache = self.cache_factory() keys = ["bacon", "spam", "eggs"] mapping = dict.fromkeys(keys, None) assert cache.set_many(mapping) == keys def test_delete_many(self): cache = self.cache_factory() keys = ["bacon", "spam", "eggs"] assert cache.delete_many(*keys) == keys def test_has(self): cache = self.cache_factory() with pytest.raises(NotImplementedError): cache.has("lobster") def test_clear(self): cache = self.cache_factory() assert cache.clear() def test_inc(self): cache = self.cache_factory() assert cache.inc("crevettes", delta=10) == 10 def test_dec(self): cache = self.cache_factory() assert cache.dec("truffle", delta=10) == -10 cachelib-0.4.1/tests/test_file_system_cache.py000066400000000000000000000052031412670761600215010ustar00rootroot00000000000000from time import sleep import pytest from clear import ClearTests from common import CommonTests from has import HasTests from cachelib import FileSystemCache @pytest.fixture(autouse=True) def cache_factory(request, tmpdir): def _factory(self, *args, **kwargs): return FileSystemCache(tmpdir, *args, **kwargs) request.cls.cache_factory = _factory class TestFileSystemCache(CommonTests, ClearTests, HasTests): # override parent sample since these must implement buffer interface sample_pairs = { "bacon": "eggs", "sausage": "spam", "brandy": "lobster", "truffle": "wine", "sauce": "truffle pate", "cravettes": "mournay sauce", } def test_EOFError(self, caplog): cache = self.cache_factory(threshold=1) assert cache.set_many(self.sample_pairs) file_names = [cache._get_filename(k) for k in self.sample_pairs.keys()] # truncate files to erase content for fpath in file_names: open(fpath, "w").close() assert cache.set("test", "EOFError") assert "Exception raised" in caplog.text def test_threshold(self): threshold = len(self.sample_pairs) // 2 cache = self.cache_factory(threshold=threshold) assert cache.set_many(self.sample_pairs) assert cache._file_count == 4 # due to autouse=True a single tmpdir is used # for each test so we need to clear it assert cache.clear() cache = self.cache_factory(threshold=0) assert cache.set_many(self.sample_pairs) assert not cache._file_count def test_file_counting(self): cache = self.cache_factory() assert cache.set_many(self.sample_pairs) assert cache._file_count == len(self.sample_pairs) assert cache.clear() assert cache._file_count == 0 def test_file_counting_on_override(self): cache = self.cache_factory() assert cache.set_many(self.sample_pairs) assert cache._file_count == len(self.sample_pairs) assert cache.set_many(self.sample_pairs) # count should remain the same assert cache._file_count == len(self.sample_pairs) def test_prune_old_entries(self): threshold = 2 * len(self.sample_pairs) - 1 cache = self.cache_factory(threshold=threshold) for k, v in self.sample_pairs.items(): assert cache.set(f"{k}-t1", v, timeout=1) assert cache.set(f"{k}-t10", v, timeout=10) sleep(3) for k, v in self.sample_pairs.items(): assert cache.set(k, v) assert cache.has(f"{k}-t10") assert not cache.has(f"{k}-t1") cachelib-0.4.1/tests/test_interface_uniformity.py000066400000000000000000000017021412670761600222600ustar00rootroot00000000000000# type: ignore import inspect import pytest from cachelib import BaseCache from cachelib import FileSystemCache from cachelib import MemcachedCache from cachelib import RedisCache from cachelib import SimpleCache @pytest.fixture(autouse=True) def create_cache_list(request, tmpdir): mc = MemcachedCache() mc._client.flush_all() rc = RedisCache(port=6360) rc._client.flushdb() request.cls.cache_list = [FileSystemCache(tmpdir), mc, rc, SimpleCache()] @pytest.mark.usefixtures("redis_server", "memcached_server") class TestInterfaceUniformity: def test_types_have_all_base_methods(self): public_api_methods = [ meth for meth in inspect.getmembers(BaseCache, predicate=inspect.isfunction) if not meth[0].startswith("_") ] for cache_type in self.cache_list: for meth in public_api_methods: assert hasattr(cache_type, meth[0]) and callable(meth[1]) cachelib-0.4.1/tests/test_memcached_cache.py000066400000000000000000000007521412670761600210700ustar00rootroot00000000000000import pytest from clear import ClearTests from common import CommonTests from has import HasTests from cachelib import MemcachedCache @pytest.fixture(autouse=True) def cache_factory(request): def _factory(self, *args, **kwargs): mc = MemcachedCache(*args, **kwargs) mc._client.flush_all() return mc request.cls.cache_factory = _factory @pytest.mark.usefixtures("memcached_server") class TestMemcachedCache(CommonTests, ClearTests, HasTests): pass cachelib-0.4.1/tests/test_redis_cache.py000066400000000000000000000007431412670761600202700ustar00rootroot00000000000000import pytest from clear import ClearTests from common import CommonTests from has import HasTests from cachelib import RedisCache @pytest.fixture(autouse=True) def cache_factory(request): def _factory(self, *args, **kwargs): rc = RedisCache(*args, port=6360, **kwargs) rc._client.flushdb() return rc request.cls.cache_factory = _factory @pytest.mark.usefixtures("redis_server") class TestRedisCache(CommonTests, ClearTests, HasTests): pass cachelib-0.4.1/tests/test_simple_cache.py000066400000000000000000000022121412670761600204440ustar00rootroot00000000000000from time import sleep import pytest from clear import ClearTests from common import CommonTests from has import HasTests from cachelib import SimpleCache @pytest.fixture(autouse=True) def cache_factory(request): def _factory(self, *args, **kwargs): return SimpleCache(*args, **kwargs) request.cls.cache_factory = _factory class TestSimpleCache(CommonTests, HasTests, ClearTests): def test_threshold(self): threshold = len(self.sample_pairs) // 2 cache = self.cache_factory(threshold=threshold) assert cache.set_many(self.sample_pairs) assert abs(len(cache._cache) - threshold) <= 1 def test_prune_old_entries(self): threshold = 2 * len(self.sample_pairs) - 1 cache = self.cache_factory(threshold=threshold) for k, v in self.sample_pairs.items(): assert cache.set(f"{k}-t0.1", v, timeout=0.1) assert cache.set(f"{k}-t5.0", v, timeout=5.0) sleep(2) for k, v in self.sample_pairs.items(): assert cache.set(k, v) assert f"{k}-t5.0" in cache._cache.keys() assert f"{k}-t0.1" not in cache._cache.keys() cachelib-0.4.1/tests/test_uwsgi_cache.py000066400000000000000000000011011412670761600203050ustar00rootroot00000000000000import pytest from clear import ClearTests from common import CommonTests from has import HasTests from cachelib import UWSGICache @pytest.fixture(autouse=True) def cache_factory(request): def _factory(self, *args, **kwargs): uwc = UWSGICache(*args, **kwargs) uwc.clear() return uwc request.cls.cache_factory = _factory class TestUwsgiCache(CommonTests, ClearTests, HasTests): pytest.importorskip( "uwsgi", reason="could not import 'uwsgi'. Make sure to " "run pytest under uwsgi for testing UWSGICache", ) cachelib-0.4.1/tox.ini000066400000000000000000000014461412670761600146000ustar00rootroot00000000000000[tox] envlist = py{39,38,37,36} style typing docs skip_missing_interpreters = true [testenv] setenv = TMPDIR={envtmpdir} deps = -r requirements/tests.txt commands = pytest -rs --capture=tee-sys --tb=short --basetemp={envtmpdir} {posargs} uwsgi --python {envbindir}/pytest --pyargv '-rs --capture=tee-sys \ --tb=short --basetemp={envtmpdir} {posargs} -kUwsgi' \ --cache2 name=default,items=100 --master python {envtmpdir}/return_pytest_exit_code.py [testenv:style] deps = pre-commit skip_install = true commands = pre-commit run --all-files --show-diff-on-failure [testenv:typing] deps = -r requirements/typing.txt commands = mypy [testenv:docs] deps = -r requirements/docs.txt commands = sphinx-build -W -b html -d {envtmpdir}/doctrees docs {envtmpdir}/html