All Projects → lukeed → Tmp Cache

lukeed / Tmp Cache

Licence: mit
A least-recently-used cache in 35 lines of code~!

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Tmp Cache

Jstarcraft Core
目标是提供一个通用的Java核心编程框架,作为搭建其它框架或者项目的基础. 让相关领域的研发人员能够专注高层设计而不用关注底层实现. 涵盖了缓存,存储,编解码,资源,脚本,监控,通讯,事件,事务9个方面.
Stars: ✭ 150 (-11.76%)
Mutual labels:  cache
Dataloader Php
DataLoaderPhp is a generic utility to be used as part of your application's data fetching layer to provide a simplified and consistent API over various remote data sources such as databases or web services via batching and caching.
Stars: ✭ 160 (-5.88%)
Mutual labels:  cache
Springbootlearning
《Spring Boot教程》源码
Stars: ✭ 2,065 (+1114.71%)
Mutual labels:  cache
Libcache
A Lightweight in-memory key:value cache library for Go.
Stars: ✭ 152 (-10.59%)
Mutual labels:  cache
Study
全栈工程师学习笔记;Spring登录、shiro登录、CAS单点登录和Spring boot oauth2单点登录;Spring data cache 缓存,支持Redis和EHcahce; web安全,常见web安全漏洞以及解决思路;常规组件,比如redis、mq等;quartz定时任务,支持持久化数据库,动态维护启动暂停关闭;docker基本用法,常用image镜像使用,Docker-MySQL、docker-Postgres、Docker-nginx、Docker-nexus、Docker-Redis、Docker-RabbitMQ、Docker-zookeeper、Docker-es、Docker-zipkin、Docker-ELK等;mybatis实践、spring实践、spring boot实践等常用集成;基于redis的分布式锁;基于shared-jdbc的分库分表,支持原生jdbc和Spring Boot Mybatis
Stars: ✭ 159 (-6.47%)
Mutual labels:  cache
Cachemanager
CacheManager is an open source caching abstraction layer for .NET written in C#. It supports various cache providers and implements many advanced features.
Stars: ✭ 2,049 (+1105.29%)
Mutual labels:  cache
Cachego
Golang Cache component - Multiple drivers
Stars: ✭ 148 (-12.94%)
Mutual labels:  cache
Http Cache Semantics
RFC 7234 in JavaScript. Parses HTTP headers to correctly compute cacheability of responses, even in complex cases
Stars: ✭ 169 (-0.59%)
Mutual labels:  cache
Action Docker Layer Caching
🐳 Enable Docker layer caching in GitHub Actions
Stars: ✭ 160 (-5.88%)
Mutual labels:  cache
Q Municate Ios
Q-municate iOS repository
Stars: ✭ 164 (-3.53%)
Mutual labels:  cache
Layercache
Caching made simple for Android and Java.
Stars: ✭ 155 (-8.82%)
Mutual labels:  cache
Cacheable Response
An HTTP compliant route path middleware for serving cache response with invalidation support.
Stars: ✭ 157 (-7.65%)
Mutual labels:  cache
Haproxy
HAProxy Load Balancer's development branch (mirror of git.haproxy.org)
Stars: ✭ 2,463 (+1348.82%)
Mutual labels:  cache
Android Cache Fix Gradle Plugin
Gradle plugin to fix Android caching problems
Stars: ✭ 150 (-11.76%)
Mutual labels:  cache
Hitchcock
The Master of Suspense 🍿
Stars: ✭ 167 (-1.76%)
Mutual labels:  cache
Strapi Middleware Cache
🔌 A cache middleware for https://strapi.io
Stars: ✭ 146 (-14.12%)
Mutual labels:  cache
Lru Cache
💫 A feature complete LRU cache implementation in C++
Stars: ✭ 162 (-4.71%)
Mutual labels:  cache
Imagepipeline
Folio Image Pipeline is an image loading and caching framework for iOS clients
Stars: ✭ 170 (+0%)
Mutual labels:  cache
Holster
A place to keep useful golang functions and small libraries
Stars: ✭ 166 (-2.35%)
Mutual labels:  cache
Xcode Clean.sh
Bash script freeing up disk space by removing Xcode generated data
Stars: ✭ 164 (-3.53%)
Mutual labels:  cache

tmp-cache CI codecov

A least-recently-used cache in 35 lines of code~!

LRU caches operate on a first-in-first-out queue. This means that the first item is the oldest and will therefore be deleted once the max limit has been reached.

When a maxAge value is set, items are given an expiration date. This allows existing items to become stale over time which, depending on your stale config, is equivalent to the item not existing at all!

In order to counteract this idle decay, all set() and get() operations on an item "refresh" its expiration date. By doing so, a new expires value is issued & the item is moved to the end of the list — aka, it's the newest kid on the block!

Install

$ npm install --save tmp-cache

Usage

const Cache = require('tmp-cache');

let cache = new Cache(3); // sets "max" size

cache.set('a', 1); //~> ['a']
cache.set('b', 2); //~> ['a', 'b']
cache.set('c', 3); //~> ['a', 'b', 'c']
cache.get('a');    //~> ['b', 'c', 'a']
cache.set('d', 4); //~> ['c', 'a', 'd']
cache.peek('a');   //~> ['c', 'a', 'd']
cache.delete('d'); //~> ['c', 'a']
cache.has('d');    //=> false
cache.set('e', 5); //~> ['c', 'a', 'e']
cache.size;        //=> 3
cache.clear();     //~> []

cache = new Cache({ maxAge:10 });

cache.set(123, 'hello'); //~> valid for 10ms
cache.get(123); //=> 'hello'  --  resets 10ms counter
setTimeout(_ => cache.get(123), 25); //=> undefined

cache = new Cache({ maxAge:0, stale:true });

cache.set('foo', [123]); //~> already stale, 0ms lifespan
cache.get('foo'); //=> [123]  --  because options.stale
cache.get('foo'); //=> undefined  --  previous op flagged removal

API

Aside from the items & changes mentioned below, tmp-cache extends the Map class, so all properties and methods are inherited.

Cache(options)

Returns: Cache extends Map

options.max

Type: Number
Default: Infinity

The maximum number of items the cache will hold. Adding more entries will force the oldest, least-recently-used item to be purged.

Failure to include any max restriction could potentially allow infinite unique entries! They will only be purged based on their expires value (if set).

Note: If options is an integer, then it is used as the options.max value.

options.maxAge

Type: Number
Default: -1

The maximum age (in ms) an item is considered valid; aka, its lifespan.

Items are not pro-actively pruned out as they age, but if you try to access an item that has expired, it will be purged and, by default, result in an undefined response.

options.stale

Type: Boolean
Default: false

Allow an expired/stale item's value to be returned before deleting it.

Cache.set(key, value, maxAge?)

Persists the item and its value into the Cache. If a maxAge value exists (via custom or cache-level options), an expiration date will also be stored.

When setting or updating an item that already exists, the original is removed. This allows the new item to be unique & the most recently used!

key

Type: String

The item's unique identifier.

value

Type: Mixed

The item's value to cache.

maxAge

Type: Number
Default: options.maxAge

Optionally override the options.maxAge for this (single) operation.

Cache.get(key, mutate?)

Retrieve an item's value by its key name. By default, this operation will refresh/update the item's expiration date.

May also return undefined if the item does not exist, or if it has expired & stale is not set.

key

Type: String

The item's unique identifier.

mutate

Type: Boolean
Default: true

Refresh the item's expiration date, marking it as more recently used.

Cache.peek(key)

Return an item's value without updating its position or refreshing its expiration date.

May also return undefined if the item does not exist, or if it has expired & stale is not set.

key

Type: String

The item's unique identifier.

License

MIT © Luke Edwards

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].