A Python dictionary implementation designed to act as an in-memory cache for FaaS environments

Overview

faas-cache-dict

A Python dictionary implementation designed to act as an in-memory cache for FaaS environments.

Formally you would describe this a memory constrained LRU TTL cache dict.

This is implementation only uses core Python stdlib with no external dependencies.

Background

This was originally designed to be used as an in-memory cache for AWS Lambda.

In most FaaS environments, successive quick calls of the function persists variables in the global scope. We can leverage this to cache data for future calls.

FaaS runtimes have limited RAM capacities so this library allows you to set a max mebibyte size for the dict. It also allows setting an optional max items length.

Items are kept in order with the LRU at the HEAD of the list.

Items are deleted if they expire, or from the head (LRU) if the cache is out of space.

Usage

Limitations

  • Performance degrades with size, you will need to test this for your use case. Though in most circumstances this will be much faster than performing a network call to a cache.
  • The library should be thread-safe, but limited testing has gone into this.

Support

CPython 3.7 or greater. No extra dependencies are required.

Contributions

This code is distributed under an open license. Feel free to fork it or preferably open a PR.

Inspirations

Thanks to mobilityhouse/ttldict for their implementation which served as a starting point.

You might also like...
Leetcode solutions - All algorithms implemented in Python 3 (for education)

Leetcode solutions - All algorithms implemented in Python 3 (for education)

This Repository consists of my solutions in Python 3 to various problems in Data Structures and Algorithms
This Repository consists of my solutions in Python 3 to various problems in Data Structures and Algorithms

Problems and it's solutions. Problem solving, a great Speed comes with a good Accuracy. The more Accurate you can write code, the more Speed you will

Python library for doing things with Grid-like structures

gridthings Python library for doing things with Grid-like structures Development This project uses poetry for dependency management, pre-commit for li

A Python library for electronic structure pre/post-processing
A Python library for electronic structure pre/post-processing

PyProcar PyProcar is a robust, open-source Python library used for pre- and post-processing of the electronic structure data coming from DFT calculati

Programming of a spanning tree algorithm with Python : In depth first with a root node.

ST Algorithm Programming of a spanning tree algorithm with Python : In depth first with a root node. Description This programm reads informations abou

Data Structure With Python

Data-Structure-With-Python- Python programs also include in this repo Stack A stack is a linear data structure that stores items in a Last-In/First-Ou

🔬 Fixed struct serialization system, using Python 3.9 annotated type hints

py-struct Fixed-size struct serialization, using Python 3.9 annotated type hints This was originally uploaded as a Gist because it's not intended as a

Final Project for Practical Python Programming and Algorithms for Data Analysis
Final Project for Practical Python Programming and Algorithms for Data Analysis

Final Project for Practical Python Programming and Algorithms for Data Analysis (PHW2781L, Summer 2020) Redlining, Race-Exclusive Deed Restriction Lan

Basic sort and search algorithms written in python.

Basic sort and search algorithms written in python. These were all developed as part of my Computer Science course to demonstrate understanding so they aren't 100% efficent

Releases(0.2.3)
  • 0.2.3(Apr 27, 2022)

    Included

    • Fix min version installable on Py 3.8 + (due to pickle protocol versioning)

    Todo:

    • Discover better documents solution

    This has not yet been tested in a prod environment, and as such should be considered beta software for now.

    Source code(tar.gz)
    Source code(zip)
  • 0.2.2(Apr 25, 2022)

    Included

    • Add purge() to empty all contents of cache dict

    Todo:

    • Discover better documents solution

    This has not yet been tested in a prod environment, and as such should be considered beta software for now.

    Source code(tar.gz)
    Source code(zip)
  • 0.2.1(Apr 22, 2022)

    Included

    • Fix set_ttl() should be able to make a value immortal by passing None
    • Fix is_expired() no longer errors if the key has been purged, now returns True if missing
    • Add ability to change the max (LRU) length of the cache once created, purging the LRU as required.
    • Add extensive new documentation

    Todo:

    • Discover better documents solution

    This has not yet been tested in a prod environment, and as such should be considered beta software for now.

    Source code(tar.gz)
    Source code(zip)
  • 0.2.0(Apr 22, 2022)

    Included

    • Add FileBackedFaaSCache
    • Add support for pickling faas cache objects
    • Bump minimum supported version to Python 3.8
    • Fix minor docs

    Todo:

    • Add full documentation

    This has not yet been tested in a prod environment, and as such should be considered beta software for now.

    Source code(tar.gz)
    Source code(zip)
  • 0.1.3(Feb 15, 2022)

    Included

    • Cleanup deprecated leftovers from objsize migration

    Todo:

    • Add full documentation

    This has not yet been tested in a prod environment, and as such should be considered beta software for now.

    Source code(tar.gz)
    Source code(zip)
  • 0.1.2(Feb 15, 2022)

    Included

    • Replaced primitive deep size algorithm with objsize

    Todo:

    • Add full documentation

    This has not yet been tested in a prod environment, and as such should be considered beta software for now.

    Source code(tar.gz)
    Source code(zip)
  • 0.0.6(Feb 15, 2022)

    Included

    • Fixed "falsey" key causing badly reported item expiry. Thanks @lokhman
    • Fixed get_deep_size incorrectly passing params on deeply nested dicts.
    • Fix handling of attempted nested deletion of already deleted items.
    • Refactor constants to own file and general code cleanup.

    Todo:

    • Add full documentation

    This has not yet been tested in a prod environment, and as such should be considered beta software for now.

    Source code(tar.gz)
    Source code(zip)
  • 0.0.5(Dec 20, 2021)

    Included

    • Fix usage in README

    Todo:

    • Add full documentation

    This has not yet been tested in a prod environment, and as such should be considered beta software for now.

    Source code(tar.gz)
    Source code(zip)
  • 0.0.4(Dec 20, 2021)

    Included

    • Add module level python version lockout
    • Improve object __repr__
    • Add usage docstring

    Todo:

    • Add full documentation

    This has not yet been tested in a prod environment, and as such should be considered beta software for now.

    Source code(tar.gz)
    Source code(zip)
  • 0.0.3(Dec 20, 2021)

    Included

    • Added new support for memory constraint shortcuts, 1M for 1 Mebibyte, 1K, 1G and 1T also supported respectively.
    • Added support for float TTL and float memory constraints.
    • Fixed nasty bug which could cause livelock if memory constraint made too small after data had been added.
    • Added max items to object __repr___.
    • Standardised user input linting.
    • Unit tests for the new functionality.
    • Excluded scripts folder from the distributable.
    • Refactoring of project to split out logic into smaller files.

    Todo:

    • Add usage documentation.

    This has not yet been tested in a prod environment, and as such should be considered beta software for now.

    Source code(tar.gz)
    Source code(zip)
  • 0.0.2(Dec 17, 2021)

    Initial release.

    Included

    • Support implemented for TTL, LRU and max mebibyte size.
    • Unit tests.

    Todo:

    • Add usage documentation

    This has not yet been tested in a prod environment, and as such should be considered beta software for now.

    Source code(tar.gz)
    Source code(zip)
Owner
Juan
Security Engineer, Python charmer, JSON wizard.
Juan
My notes on Data structure and Algos in golang implementation and python

My notes on DS and Algo Table of Contents Arrays LinkedList Trees Types of trees: Tree/Graph Traversal Algorithms Heap Priorty Queue Trie Graphs Graph

Chia Yong Kang 0 Feb 13, 2022
A Python implementation of red-black trees

Python red-black trees A Python implementation of red-black trees. This code was originally copied from programiz.com, but I have made a few tweaks to

Emily Dolson 7 Oct 20, 2022
RLStructures is a library to facilitate the implementation of new reinforcement learning algorithms.

RLStructures is a lightweight Python library that provides simple APIs as well as data structures that make as few assumptions as possibl

Facebook Research 262 Nov 18, 2022
Data Structures and algorithms package implementation

Documentation Simple and Easy Package --This is package for enabling basic linear and non-linear data structures and algos-- Data Structures Array Sta

null 1 Oct 30, 2021
Array is a functional mutable sequence inheriting from Python's built-in list.

funct.Array Array is a functional mutable sequence inheriting from Python's built-in list. Array provides 100+ higher-order methods and more functiona

null 182 Nov 21, 2022
A DSA repository but everything is in python.

DSA Status Contents A: Mathematics B: Bit Magic C: Recursion D: Arrays E: Searching F: Sorting G: Matrix H: Hashing I: String J: Linked List K: Stack

Shubhashish Dixit 63 Dec 23, 2022
Common sorting algorithims in Python

This a Github Repository with code for my attempts for commonly used sorting algorithims, tested on a list with 3000 randomly generated numbers.

Pratham Prasoon 14 Sep 2, 2021
pyprobables is a pure-python library for probabilistic data structures

pyprobables is a pure-python library for probabilistic data structures. The goal is to provide the developer with a pure-python implementation of common probabilistic data-structures to use in their work.

Tyler Barrus 86 Dec 25, 2022
Python collections that are backended by sqlite3 DB and are compatible with the built-in collections

sqlitecollections Python collections that are backended by sqlite3 DB and are compatible with the built-in collections Installation $ pip install git+

Takeshi OSOEKAWA 11 Feb 3, 2022
This repository is a compilation of important Data Structures and Algorithms based on Python.

Python DSA ?? This repository is a compilation of important Data Structures and Algorithms based on Python. Please make seperate folders for different

Bhavya Verma 27 Oct 29, 2022