Skip to main content

A simplest and thread-safe LRU cache, which support key-func, release-func and hit-stat.

Project description

XCache: A simplest and thread-safe LRU cache, which support key-func, release-func and hit-stat.

Setup

pip install xcache-lib

Key Features

  • LRU cache
  • Thread safe
  • Support special key function
  • Support special release function
  • Has detail hit stat

Core Function Descriptor: xcache

@xcache(cache_size, key_func=None, release_func=None, log_keys=False)
Param Value Description
cache_size size of cache storage
key_fun special key function
release_func special release function
log_keys log keys, for DEBUG

Quick Start

from xcache import xcache, show_xcache_hit_stat


@xcache(3, key_func=lambda x,y: x, log_keys=True)
def calc(x, y):
    print("calc {}, {}".format(x, y))
    return x + y


if __name__ == "__main__":
    print(calc(5, 6))
    print(calc(5, 6))
    print(calc(5, 6))
    print(calc(5, 5))
    print(calc(4, 6))
    print(calc(3, 6))
    print(calc(2, 6))
    print(calc(1, 6))
    print(calc(9, 6))

    print(json.dumps(show_xcache_hit_stat(x), indent=4))  # show detail hit stat

outputs:

calc 5, 6
11
11
11
calc 5, 5
10
calc 4, 6
10
calc 3, 6
9
calc 2, 6
8
calc 1, 6
7
calc 9, 6
15
{
    "hit_rate": 22.22222222222222, 
    "dup_calc_rate": 0.6666666666666666, 
    "hit": [
        2, 
        7
    ], 
    "keys_num": 1, 
    "detail": {
        "key_detail": {
            "1": 6, 
            "3": 1
        }, 
        "hit_detail": {
            "2": 1
        }
    }
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xcache-lib-0.0.2.tar.gz (2.8 kB view details)

Uploaded Source

File details

Details for the file xcache-lib-0.0.2.tar.gz.

File metadata

  • Download URL: xcache-lib-0.0.2.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.3

File hashes

Hashes for xcache-lib-0.0.2.tar.gz
Algorithm Hash digest
SHA256 dbf1e3eb659a5876b03e05c062a8443a0241040509b7d008988d10a29261f7d1
MD5 fe0367eb6cda3fca204d06a58609dbc4
BLAKE2b-256 5babce001cd5fd4de1ec5a2c0fb88e013df6f284b3405c22a1236bed3b5cdbff

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page