Skip to content

Add limited size memory cache backend#1039

Open
lucasmrdt wants to merge 1 commit into
aio-libs:masterfrom
lucasmrdt:master
Open

Add limited size memory cache backend#1039
lucasmrdt wants to merge 1 commit into
aio-libs:masterfrom
lucasmrdt:master

Conversation

@lucasmrdt
Copy link
Copy Markdown

What do these changes do?

Adds a new LimitedSizeMemoryCache backend that extends SimpleMemoryCache with LRU (Least Recently Used) eviction policy and configurable size limits. The cache automatically evicts least recently used items when the size limit is exceeded.

Are there changes in behavior for the user?

Yes, users now have access to a memory-constrained cache backend. The new cache:

  • Accepts a max_size_mb parameter (default: 64MB) to limit total cache size
  • Uses LRU eviction when the size limit is reached
  • Optionally raises MemoryError when trying to cache items larger than the limit (via raise_on_oversize=True)
  • Is available as aiocache.LimitedSizeMemoryCache and included in the default cache backends list

Related issue number

None

Checklist

  • I think the code is well written
  • Unit tests for the changes exist (in tests/ut/backends/test_limited_memory.py)
  • Documentation reflects the changes (in docs/caches.rst)

@Dreamsorcerer
Copy link
Copy Markdown
Member

Dreamsorcerer commented Aug 2, 2025

This seems too close to #1027. I think anything more complex should be done in an external library and linked from the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants