Package: lru-expiring-cache
# LRU Expiring Cache[![Build Status](https://travis-ci.org/vpaliy/lru-cache.svg?branch=master)](https://travis-ci.org/vpaliy/lru-cache)[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)[![codecov](https://codecov.io/gh/vpaliy/lru-cache/branch/master/graph/badge.svg)](https://codecov.io/gh/vpaliy/lru-cache)This repository contains a dictionary-like data structure, supporting LRU caching semantics and data expiration mechanism. You can add a new record to the cache and assign an expiration time for that record. Records are not required to have the same "life span": you can mix them up, and it will still work.### How does it work?LRU cache uses a daemon thread - AKA cache cleaner - to silently clean up expired items in the background. The daemon thread receives proxied objects from a shared queue, picks up the one with the shortest life span, and uses a condition variable to wait until the record expires.### Install`pip install lru-expiring-cache`or:```$ git clone https://github.com/vpaliy/lru-expiring-cache.git$ cd lru-expiring-cache/```### Usage```pythonimport timefrom  lru import LruCache# every record will expire in 5 seconds unless otherwise specifiedcache = LruCache(maxsize=10, expires=5)cache['foo'] = 'bar'print(cache['foo']) # prints 'bar'# sleep for 5 minutestime.sleep(5)print(cache['foo']) # KeyError# adding a new item that expires in 10 secondscache.add(key='foo', value='bar', expires=10)# deletingdel cache['foo']```To make the LRU cache thread-safe, just pass `concurrent=True` when constructing a new instance:```pythonfrom lru import LruCachecache = LruCache(maxsize=10, concurrent=True)```Note: LRU cache extends the `MutableMapping` interface from the standard library; therefore it supports all methods inherent to the standard mapping types in Python.Additionally, you can use cache decorators:- `lru_cache(maxsize, expires)`- `lazy_cache(maxsize, expires)`Both are memoization decorators that support data expiration. The difference is that `lru_cache` uses `LruCache` (obviously) under the hood, and `lazy_cache` uses the native `dict`.For example, using `lazy_cache` is super easy:```pythonimport timefrom  lru import lazy_cache# each new item will expire in 10 seconds@lazy_cache(maxsize=10, expires=10)def function(first, second, third):# simulate performing a computationaly expensive tasktime.sleep(10)return first + second + thirdfunction(10, 10, 10) # sleeps for 10 seconds and returns 30function(10, 10, 10) # returns 30 instantaneouslytime.sleep(10) # wait until expiresfunction(10, 10, 10) # sleeps for 10 seconds because all cached results have expired```Which one to use?If your function requires the functionality of LRU cache (removing the least recently used records to give room to the new ones), then use `lru_cache`; otherwise if you just need an expiring caching mechaniism, use `lazy_cache`. Note that `lazy_cache` clears the entire cache when the number of records have reached `maxsize`.## License```MIT LicenseCopyright (c) 2019 Vasyl PaliyPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.```