Package: cachebox
CacheboxCacheboxis a Python library (written in Rust) that provides memoizations and cache implementions with
different cache replecement policies.[!NOTE]This library is faster than other libraries (between 5x-10x) and uses lower memory than them,you can see benchmarks here.fromcacheboximportcached,TTLCache,LRUCache# Keep coin price for no longer than a minute@cached(TTLCache(maxsize=126,ttl=60))defget_coin_price(coin_name):returnweb3_client.get_price(coin_name)# Async functions are supported@cached(LRUCache(maxsize=126))asyncdeffib(n):returnnifn<2elsefib(n-1)+fib(n-2)# You can pass `capacity` parameter.# If `capacity` specified, the cache will be able to hold at least capacity elements without reallocating.@cached(LRUCache(maxsize=126,capacity=100))defget_coin_price(coin_name):returnweb3_client.get_price(coin_name)Page Content:Should i use memoization?FeaturesInstallationTutorialdifference betweenTTLCacheandTTLCacheNoDefaultFrequently Asked QuestionsWhat is caching and why to use it?Wikipeda:In computing, caching improves performance by keeping recent or often-used data
items in memory locations which are faster, or computationally cheaper to access,
than normal memory stores. When the cache is full, the algorithm must choose which
items to discard to make room for new data.Researchgate:Cache replacement policies play important roles in efficiently processing the current
big data applications. The performance of any high performance computing system is highly
depending on the performance of its cache memory.FeaturesPros:Thread-safe (uses RustsRwLock)You can use itasyncandsyncVarius cache alghoritms (supports8cache alghoritms)Super fast (is written in Rust language)High performanceCons:Does not support iterating (forvalues,keysanditemsmethods)Supported:Cache: Simple cache implemention with no policy and alghoritm.FIFOCache: First In First Out cache implemention.LFUCache: Least Frequently Used cache implemention.RRCache: Random Replacement cache implemention.LRUCache: Least Recently Used cache implemention.MRUCache: Most Recently Used cache implemention.TTLCache: LRU Cache Implementation With Per-Item TTL Value.TTLCacheNoDefault: Time-aware Cache Implemention; With this cache, you can set its own expiration time for each key-value pair.InstallationYou can installcacheboxfrom PyPi:pip3install-UcacheboxNow you can use it:>>>importcachebox>>>cachebox.__version__'...'TutorialThis package is very easy to use. You can use all implementions like a dictionary;
they're supported allabc.MutableMappingmethods.
Butthere are some new methodsyou can see in examples.At first, think about which alghoritm do you want to use? import and use it like a dictionary; In this examples we usedLRUCache,TTLCacheandTTLCacheNoDefault.LRUCacheexample:fromcacheboximportLRUCachecache=LRUCache(10)cache.insert("key","value")# or cache["key"] = "value"cache.delete("key")# or `del cache["key"]`# `.clear()` method has new parameter `reuse`# pass True to keeps the allocated memory for reuse (default False).cache.clear(reuse=True)And there are new methods forTTLCacheandTTLCacheNoDefault. You can see those methods
in these examples:TTLCacheexample:fromcacheboximportTTLCachecache=TTLCache(10,ttl=2)cache.insert(1,"value1")# or cache[1] = "value1"cache.insert(2,"value2")# or cache[2] = "value2"cache.insert(3,"value3")# or cache[3] = "value3"# It works like `.get()` with the difference that it returns the expiration of item in seconds.cache.get_with_expire(1)# Output: ('value1', 1.971873426437378)# It works like `.popitem()` with the difference that it returns the expiration of item in seconds.cache.popitem_with_expire()# Output: (1, 'value1', 1.961873426437378)# It works like `.pop()` with the difference that it returns the expiration of item in seconds.cache.pop_with_expire(2)# Output: ('value2', 1.951873426437378)# Calling this method removes all items whose time-to-live would have expired by time,# and if `reuse` be True, keeps the allocated memory for reuse (default False).cache.expire(reuse=False)TTLCacheNoDefaultexample:fromcacheboximportTTLCacheNoDefault# TTLCacheNoDefault have not ttl parameter here.cache=TTLCacheNoDefault(10)cache.insert(1,"value1",ttl=10)# this key-pair is available for no longer than 10 secondscache.insert(2,"value2",ttl=2)# this key-pair is available for no longer than 2 secondscache.setdefault(3,"value3",ttl=6)# this key-pair is available for no longer than 6 secondscache.insert(4,"value4",ttl=None)# this key-pair never expire# It works like `.get()` with the difference that it returns the expiration of item in seconds.cache.get_with_expire(1)# Output: ('value1', 9.971873426437378)# It works like `.popitem()` with the difference that it returns the expiration of item in seconds.cache.popitem_with_expire()# Output: (2, 'value2', 1.961873426437378)# It works like `.pop()` with the difference that it returns the expiration of item in seconds.cache.pop_with_expire(4)# Output: ('value4', 0.0)Frequently asked questionsWhat is the difference between TTLCache and TTLCacheNoDefault?InTTLCache, you set an expiration time for all items, but inTTLCacheNoDefault,
you can set a unique expiration time for each item.TTLSpeedTTLCacheOne ttl for all itemsTTLCache is very faster than TTLCacheNoDefaultTTLCacheNoDefaultEach item has unique expiration timeTTLCacheNoDefault is very slow in insertingCan we set maxsize to zero?Yes, if you pass zero to maxsize, means there's no limit for items.I use cachetools, how to change it to cachebox?cacheboxsyntax is very similar tocachetools.
Just change these items:# If you use `isinstance` for cachetools classes, change those.isinstance(cache,cachetools.Cache)->isinstance(cache,cachebox.BaseCacheImpl)# If you pass `None` to `cached()`, change it to `dict`.@cachetools.cached(None)->@cachebox.cached({})# If you use `cache.maxsize`, change it to `cache.getmaxsize()`cache.maxsize->cache.getmaxsize()LicenseCopyright (c) 2024 aWolverP -MIT License