Skip to main content

Object Cache

Size-bounded object cache with pluggable eviction policies.

Overview

VCollab applications frequently need to hold computed or loaded objects in memory with a bounded size budget. When the budget is exceeded, old entries must be evicted according to a configurable policy.

The vcti-cache package provides ObjectCache, a generic, size-bounded cache with a pluggable eviction policy defined by the abstract CachePolicy base class. Implement CachePolicy to control how item sizes are calculated and which items are evicted first.

This package has zero external dependencies.


Installation

From PyPI

pip install vcti-cache

In requirements.txt

vcti-cache>=1.2.0

In pyproject.toml dependencies

dependencies = [
    "vcti-cache>=1.2.0",
]

Quick Start

from vcti.cache import ObjectCache, LruPolicy

# 1. Create a cache with a built-in LRU policy
cache = ObjectCache[str](max_size=100, cache_policy=LruPolicy(get_size=len))

# 2. Add, retrieve, and remove items
cache.add("greeting", "hello")
print(cache.get("greeting"))   # "hello"
print(cache["greeting"])       # "hello"  (bracket syntax)
print("greeting" in cache)     # True
print(len(cache))              # 1
print(cache.current_size)      # 5

# 3. Update (upsert) an existing item
cache.update("greeting", "hi there")
print(cache.get("greeting"))   # "hi there"

# 4. Inspect statistics
print(cache.stats.hits)        # 2  (from the get + bracket access)
print(cache.stats.adds)        # 2  (original add + update)

cache.remove("greeting")
print(cache.get("greeting"))   # None

Five bundled policies are available — FifoPolicy, LruPolicy, LfuPolicy, MruPolicy, and RrPolicy — each accepting a get_size callable. For custom eviction logic, subclass CachePolicy[T] directly (see Extending).


Thread Safety

ObjectCache is not thread-safe. If you share a cache across threads, wrap it with an external lock (e.g. threading.Lock).


Public API

Symbol Kind Description
ObjectCache[T] Class Size-bounded cache with eviction support
CachePolicy[T] ABC Abstract base for eviction strategies
FifoPolicy[T] Policy First-in-first-out eviction
LruPolicy[T] Policy Least-recently-used eviction
LfuPolicy[T] Policy Least-frequently-used eviction
MruPolicy[T] Policy Most-recently-used eviction (scan-resistant)
RrPolicy[T] Policy Random replacement (Bélády-immune)
CacheStats Dataclass Immutable hit/miss/eviction/removal statistics
CacheOverflowError Exception Raised when an item cannot fit

ObjectCache[T]

Method / Property Description
add(key, value) Insert an item; evicts if needed, raises on duplicate key
update(key, value) Insert or replace an item (upsert); evicts if needed
get(key) Retrieve by key or None
cache[key] Retrieve by key or raise KeyError
remove(key) Remove a single item (fires on_remove)
clear() Remove all items
keys() List of current keys
values() List of current values
items() List of (key, value) pairs
iter(cache) Iterate over keys
max_size Maximum capacity in bytes
current_size Total size of cached items (bytes)
get_item_size(key) Cached size of an item (bytes), or None if missing
stats CacheStats instance with hit/miss/eviction counters
policy The active CachePolicy instance
key in cache Membership test
len(cache) Number of cached items

CachePolicy[T] (abstract)

Method Override Description
get_size(item) Required Return item size in bytes (>= 0)
get_eviction_items(space, items) Required Return keys to evict
on_add(key, item) Optional Called after an item is added
on_access(key, item) Optional Called when an item is retrieved
on_evict(key, item) Optional Called when an item is evicted by the policy
on_remove(key, item) Optional Called when an item is explicitly removed by the caller

CacheStats

Field Description
hits Successful get / [] calls
misses get calls returning None / [] raising KeyError
adds Successful add / update insertions
evictions Items removed by the eviction policy
removals Items explicitly removed by remove() / clear() / update() replacement

Bundled Policies

All bundled policies accept get_size: Callable[[T], int] in their constructor.

Policy Eviction Rule Internal Structure
FifoPolicy[T] Oldest insertion first OrderedDict
LruPolicy[T] Least recently accessed OrderedDict (moves on access)
LfuPolicy[T] Lowest access count (ties: oldest) defaultdict(int) + OrderedDict
MruPolicy[T] Most recently accessed OrderedDict (evicts from end)
RrPolicy[T] Random (accepts optional seed) list + random.Random

Start with LruPolicy — it's the best general-purpose default. See Choosing a Policy for a detailed comparison of performance, trade-offs, and when to use each.


Dependencies

None. vcti-cache is pure Python with zero runtime dependencies.


Documentation

If you want to… Read
Get started using the package Quick Start above
Understand the architecture and design decisions docs/design.md
Navigate and understand the source docs/source-guide.md
See practical, real-world usage docs/patterns.md
Write a custom eviction policy docs/extending.md
Look up a specific class or method docs/api.md

Download files

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

Source Distribution

vcti_cache-1.2.0.tar.gz (23.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

vcti_cache-1.2.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file vcti_cache-1.2.0.tar.gz.

File metadata

  • Download URL: vcti_cache-1.2.0.tar.gz
  • Upload date:
  • Size: 23.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for vcti_cache-1.2.0.tar.gz
Algorithm Hash digest
SHA256 088a73dc27dd2efd6ec893f13580d8cf002a894f49107de131817e5780623260
MD5 0b79071c035490424baaec55f0cd2c2e
BLAKE2b-256 d2662a8f3a7fea051e9c8207e5f481452deeff68e13a4e8c822903089417afac

See more details on using hashes here.

Provenance

The following attestation bundles were made for vcti_cache-1.2.0.tar.gz:

Publisher: release.yml on vcollab/vcti-python-cache

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file vcti_cache-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: vcti_cache-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for vcti_cache-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bf7f2163898535d5493852acc0bcb20dcdcc0e2a0ac7381848eae3b0526adad4
MD5 bb7403d31c84ec167d3a33d09323a420
BLAKE2b-256 841caba4ded764d9d04b990f5594676f429433b9e157662f6e57bd3b75c9e64e

See more details on using hashes here.

Provenance

The following attestation bundles were made for vcti_cache-1.2.0-py3-none-any.whl:

Publisher: release.yml on vcollab/vcti-python-cache

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 files

1.1.1

2 files

Supported by

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