Skip to main content

HiLCaT

High level key-value storage and cache tool.

Installation

pip install hilcat

Usage

Cache api is designed to determine a unique node by scope and key.

In some implements, scope may be always None and should be ignored, thus unique node determined only by key.

Init by different backends

auto backend

create a cache based on redis

from hilcat import Cache

cache = Cache.from_uri('redis://localhost:1458')

create a cache based on sqlite

from hilcat import Cache

cache = Cache.from_uri('sqlite:///t.db')

in memory cache

from hilcat import MemoryCache

cache = MemoryCache()

file based cache

cache text content in file

from hilcat import SimpleTextFileCache

cache = SimpleTextFileCache(root_dir='cache_dir', suf='.txt')

redis

init from url

from hilcat import RedisCache

cache = RedisCache(url='redis://localhost:6379')

init from host

from hilcat import RedisCache

cache = RedisCache(host='localhost', port=6379)

elasticsearch

from hilcat import ElasticSearchCache

cache = ElasticSearchCache(hosts=['https://localhost:9200'])

sqlite

from hilcat import SqliteCache, RelationalDbScopeConfig

cache = SqliteCache(database=db_file, scopes=[
    RelationalDbScopeConfig(scope='a', uniq_columns=['id'], columns=['id', 'name', 'comment', 'count'],
                            column_types={'count': 'int'}),
    RelationalDbScopeConfig(scope='b', uniq_columns=['eid'], columns=['eid', 'name', 'comment', 'status'])
])

postgresql

from hilcat import PostgresqlCache, RelationalDbScopeConfig

cache = PostgresqlCache(database="postgresql://postgres:123@localhost:5432/hilcat_test", scopes=[
    RelationalDbScopeConfig(scope='a', uniq_columns=['id'], columns=['id', 'name', 'comment', 'count'],
                            column_types={'count': 'int'}),
    # RelationalDbScopeConfig(scope='b', uniq_columns=['eid'], columns=['eid', 'name', 'comment', 'status'])
])

mysql

from hilcat import MysqlCache, RelationalDbScopeConfig

cache = MysqlCache(connection=connection, scopes=[
    RelationalDbScopeConfig(scope='a', uniq_columns=['id'], columns=['id', 'name', 'comment', 'count'],
                            column_types={'id': 'varchar(50)', 'count': 'int'}),
    RelationalDbScopeConfig(scope='b', uniq_columns=['eid'], columns=['eid', 'name', 'comment', 'status'],
                            column_types={'eid': "int"})
])

cache api

Assume there is a cache named cache.

exists(key, scope=None)

Test if a key exists in cache for certain scope.

fetch(key, default=None, scope=None)

If key not exists, return default value.

value = cache.fetch('one', 1, scope='a')

set(key, value, scope=None)

cache.set('one', 1, scope='a')

update(key, value, scope=None, **kwargs)

Same as method set, but return value may diff in some implements.

get(key, func=None, scope=None)

If key exists, just return value stored in cache; else if key not exists, calculate value and store to cache, the return value.

value = cache.get('one', lambda: 1, scope='a')

pop(key, scope=None)

Delete value of given key for certain scope.

scopes()

Get all scopes in the cache.

May not supported for some implements.

keys(scope=None)

Get all keys for certain scope.

May not supported for some implements.

load(scopes=None)

Load scope data from persistence storage.

Some implements may have no persistence storage, thus this method do nothing.

backup(scopes=None)

Save scope data to persistence storage.

Some implements may have no persistence storage, thus this method do nothing.

Decorate a function

import collections
from hilcat import SqliteCache, RelationalDbScopeConfig

db_file = "decorator.db"
cache = SqliteCache(database=db_file, scopes=[
    RelationalDbScopeConfig(scope='f1', uniq_columns=['x'], columns=['y']),
    RelationalDbScopeConfig(scope='f3', uniq_columns=['key'], columns=['key', 'value'])
])

c1 = collections.Counter()
@cache(scope="f1")
def f1(x: int):
    c1[x] += 1
    return x + 1 + c1[x]

c2 = collections.Counter()
def f2(x: int):
    c2[x] += 1
    return x + 1 + c2[x]

def make_key(x: int, y: int):
    return '-'.join(map(str, [x, y]))
c3 = collections.Counter()
@cache(scope="f3", make_key_func=make_key)
def f3(x: int, y: int):
    c3[(x, y)] += 1
    return {
        "key": make_key(x, y),
        "value": x + y + c3[(x, y)],
    }

# with cache, same result
assert f1(1) == 3
assert f1(1) == 3

# without cache, different result
assert f2(1) == 3
assert f2(1) == 4

assert f3(1, 2) == {"key": "1-2", "value": 4}
assert f3(1, 2) == {"key": "1-2", "value": 4}

Metadata

Release files for hilcat 1.4.0

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

Source distribution (sdist)

Source distribution for hilcat 1.4.0
File Size Uploaded Provenance Actions
hilcat-1.4.0.tar.gz 24.8 kB Without provenance Details for hilcat-1.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hilcat 1.4.0
File Interpreter ABI Platform Actions
hilcat-1.4.0-py3-none-any.whl Python 3 none any Details for hilcat-1.4.0-py3-none-any.whl

Total release size: 54.3 kB

Release files / hilcat-1.4.0.tar.gz

Download URL hilcat-1.4.0.tar.gz
Size 24.8 kB
Tags Source
SHA-256 checksum
How to use checksums
4e46c580e4491a8121f7ed96680efe3a474e1b790971f538f918218c0570b7dd
BLAKE2b-256 checksum
How to use checksums
d127e526b6cfde129810400c5500c4cc058155468959f62bdb655cf78eb07cfa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / hilcat-1.4.0-py3-none-any.whl

Download URL hilcat-1.4.0-py3-none-any.whl
Size 29.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dcb0fc0ba44825fdc91edc41ac2cbbde66b9c3622e90bac766526303a4210b30
BLAKE2b-256 checksum
How to use checksums
cd19e8c383c8cedb94fb77e12adff3195454431192897a0d862c3e3d5da5f5b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Security for hilcat 1.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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