未提供项目说明
lc-cache的Python项目详细描述
缓存py
概述
缓存可用作API响应的临时、快速访问数据存储、使用复杂计算计算的数据,以及
检索/计算相对昂贵的任何其他类型的数据。cache-py
是一个Python库,包含
一个简单的缓存接口(基类),使用这个接口的公共缓存的实现,以及一个通用的
Python对象的哈希函数。所有的实现目前都是本地的和内存中的,尽管可以很容易地使用接口
实现基于文件或API的缓存。在
安装
从PyPi安装(首选方法)
pip install lc-cache
使用
^{pr2}$
其中x.x.x
是要下载的版本。在
手动下载安装
要下载源分发和/或控制盘文件,请导航到
https://github.com/libcommon/cache-py/tree/releases/vx.x.x/dist
,其中x.x.x
是要安装的版本,
并通过用户界面或wget等工具进行下载。然后安装运行:
pip install <downloaded file>
下载后请不要更改文件名,因为Pip需要安装文件的特定命名约定。在
依赖关系
cache-py
没有外部依赖项。官方只支持Python>;=3.6版本。在
入门
如果您只需要存储值并稍后检查它们的存在,但不一定关心检索值本身,请使用HashsetCache
。在
importrandomfromlc_cacheimportHashsetCachedefmain()->int:# Create the cachecache=HashsetCache()# Insert some random values into the cachefor_inrange(10):cache.insert(random.randrange(0,10))# Check for their presenceforiinrange(10):ifcache.check(i):print("Encountered value",i)return0
如果以后需要检索值,并且有一个键将它们关联起来,请使用HashmapCache
。下面是一个实现
尽管它只是一个引用。不要使用此实现,因为它不是线程安全的,并且
functools
版本有一些优化,可能会使它更快。在
fromfunctoolsimportwrapsfromtypingimportAny,Callablefromlc_cacheimportHashmapCache,gen_python_hash_CACHE=HashmapCache()deflru_cache(f:Callable[...,Any])->Callable[...,Any]:@wraps(f)defwrapper(*args,**kwargs)->Any:args_hash=hash((gen_python_hash(args),gen_python_hash(kwargs)))# NOTE: Have to use check here because a cached value may be Noneif_CACHE.check(args_hash):return_CACHE.get(args_hash)returnf(*args,**kwargs)returnwrapper
SizedHashmapCache
和SizedLRUCache
类在需要对
缓存中的项目数。在
用例
缓存接口类Cache
定义了一个用于操作底层存储对象的简单API:
* `check`: check if a value is present in the cache
* `insert`: add a value to the cache, or update an existing one
* `get`: get a value from the cache, if it exists
* `remove`: remove a value from the cache, if it exists
* `clear`: clear the cache
* `iter`: returns a [Generator](https://wiki.python.org/moin/Generators) over the values in the dictionary
请注意,这里的“value”一词使用松散,并不是指Python字典中的值。话虽如此,
Python字典通常用于缓存值。HashmapCache
有一个常规的Python字典作为底层
存储对象,因此可以像字典一样使用。类似地,SizedHashmapCache
和{}缓存
类也有字典支持,但对缓存大小和删除项的逻辑有一些额外的限制。在
如果你的用例并不复杂,一个简单的字典就足够了,那么就没有必要使用这个库中的类。
但是,如果您需要类似LRU缓存的东西,或者如果您正在开发MVP,但需要更复杂的缓存实现
以后(因此需要一个一致的API),这个库可能会有所帮助。在
贡献/建议
欢迎投稿和建议!要提出功能请求,请报告错误,或对现有的
功能,请file an issue。请投稿
submit a PR,但请确保lint、键入check并测试代码
在这样做之前。提前谢谢!在
标签:
- 项目
推荐PyPI第三方库
其中x.x.x
是要下载的版本。在
手动下载安装
要下载源分发和/或控制盘文件,请导航到
https://github.com/libcommon/cache-py/tree/releases/vx.x.x/dist
,其中x.x.x
是要安装的版本,
并通过用户界面或wget等工具进行下载。然后安装运行:
pip install <downloaded file>
下载后请不要更改文件名,因为Pip需要安装文件的特定命名约定。在
依赖关系
cache-py
没有外部依赖项。官方只支持Python>;=3.6版本。在
入门
如果您只需要存储值并稍后检查它们的存在,但不一定关心检索值本身,请使用HashsetCache
。在
importrandomfromlc_cacheimportHashsetCachedefmain()->int:# Create the cachecache=HashsetCache()# Insert some random values into the cachefor_inrange(10):cache.insert(random.randrange(0,10))# Check for their presenceforiinrange(10):ifcache.check(i):print("Encountered value",i)return0
如果以后需要检索值,并且有一个键将它们关联起来,请使用HashmapCache
。下面是一个实现
尽管它只是一个引用。不要使用此实现,因为它不是线程安全的,并且
functools
版本有一些优化,可能会使它更快。在
fromfunctoolsimportwrapsfromtypingimportAny,Callablefromlc_cacheimportHashmapCache,gen_python_hash_CACHE=HashmapCache()deflru_cache(f:Callable[...,Any])->Callable[...,Any]:@wraps(f)defwrapper(*args,**kwargs)->Any:args_hash=hash((gen_python_hash(args),gen_python_hash(kwargs)))# NOTE: Have to use check here because a cached value may be Noneif_CACHE.check(args_hash):return_CACHE.get(args_hash)returnf(*args,**kwargs)returnwrapper
SizedHashmapCache
和SizedLRUCache
类在需要对
缓存中的项目数。在
用例
缓存接口类Cache
定义了一个用于操作底层存储对象的简单API:
* `check`: check if a value is present in the cache
* `insert`: add a value to the cache, or update an existing one
* `get`: get a value from the cache, if it exists
* `remove`: remove a value from the cache, if it exists
* `clear`: clear the cache
* `iter`: returns a [Generator](https://wiki.python.org/moin/Generators) over the values in the dictionary
请注意,这里的“value”一词使用松散,并不是指Python字典中的值。话虽如此,
Python字典通常用于缓存值。HashmapCache
有一个常规的Python字典作为底层
存储对象,因此可以像字典一样使用。类似地,SizedHashmapCache
和{
如果你的用例并不复杂,一个简单的字典就足够了,那么就没有必要使用这个库中的类。 但是,如果您需要类似LRU缓存的东西,或者如果您正在开发MVP,但需要更复杂的缓存实现 以后(因此需要一个一致的API),这个库可能会有所帮助。在
贡献/建议
欢迎投稿和建议!要提出功能请求,请报告错误,或对现有的 功能,请file an issue。请投稿 submit a PR,但请确保lint、键入check并测试代码 在这样做之前。提前谢谢!在
- 项目
标签: