All Projects → withspectrum → Redis Tag Cache

withspectrum / Redis Tag Cache

Licence: mit
Cache and invalidate records in Redis with tags

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Redis Tag Cache

Aiocache
Asyncio cache manager for redis, memcached and memory
Stars: ✭ 496 (+933.33%)
Mutual labels:  redis, cache
Gocache
☔️ A complete Go cache library that brings you multiple ways of managing your caches
Stars: ✭ 775 (+1514.58%)
Mutual labels:  redis, cache
Laravel Eloquent Query Cache
Adding cache on your Laravel Eloquent queries' results is now a breeze.
Stars: ✭ 529 (+1002.08%)
Mutual labels:  redis, cache
Stackexchange.redis.extensions
Stars: ✭ 419 (+772.92%)
Mutual labels:  redis, cache
Weixinmpsdk
微信全平台 SDK Senparc.Weixin for C#,支持 .NET Framework 及 .NET Core、.NET 6.0。已支持微信公众号、小程序、小游戏、企业号、企业微信、开放平台、微信支付、JSSDK、微信周边等全平台。 WeChat SDK for C#.
Stars: ✭ 7,098 (+14687.5%)
Mutual labels:  redis, cache
Lada Cache
A Redis based, fully automated and scalable database cache layer for Laravel
Stars: ✭ 424 (+783.33%)
Mutual labels:  redis, cache
Synchrotron
Caching layer load balancer.
Stars: ✭ 42 (-12.5%)
Mutual labels:  redis, cache
Ssm booksystem
ssm demo,ssm详细教程,SSM简明教程:简单的十步教你搭建人生第一个SSM框架[ SSM框架整合教程(spring+spring mvc+mybatis+redis+maven+idea+bootstrap) ]
Stars: ✭ 355 (+639.58%)
Mutual labels:  redis, cache
Go Reflectx
Go reflection library to find struct field by its tag
Stars: ✭ 19 (-60.42%)
Mutual labels:  cache, tags
Java Knowledge Mind Map
【🌱🌱Java服务端知识技能图谱】用思维脑图梳理汇总Java服务端知识技能
Stars: ✭ 787 (+1539.58%)
Mutual labels:  redis, cache
Ledge
An RFC compliant and ESI capable HTTP cache for Nginx / OpenResty, backed by Redis
Stars: ✭ 412 (+758.33%)
Mutual labels:  redis, cache
Exchange Rates
💱 Querying a rate-limited currency exchange API using Redis as a cache
Stars: ✭ 37 (-22.92%)
Mutual labels:  redis, cache
Ring
Python cache interface with clean API and built-in memcache & redis + asyncio support.
Stars: ✭ 404 (+741.67%)
Mutual labels:  redis, cache
Redis
Vapor provider for RediStack
Stars: ✭ 434 (+804.17%)
Mutual labels:  redis, cache
Dynomite
A generic dynamo implementation for different k-v storage engines
Stars: ✭ 3,830 (+7879.17%)
Mutual labels:  redis, cache
Bloom
🌸 HTTP REST API caching middleware, to be used between load balancers and REST API workers.
Stars: ✭ 553 (+1052.08%)
Mutual labels:  redis, cache
Hibernate Redis
hibernate 2nd level cache privder using redis
Stars: ✭ 345 (+618.75%)
Mutual labels:  redis, cache
Kache
A simple in memory cache written using go
Stars: ✭ 349 (+627.08%)
Mutual labels:  redis, cache
Smartsql
SmartSql = MyBatis in C# + .NET Core+ Cache(Memory | Redis) + R/W Splitting + PropertyChangedTrack +Dynamic Repository + InvokeSync + Diagnostics
Stars: ✭ 775 (+1514.58%)
Mutual labels:  redis, cache
Apicache
Simple API-caching middleware for Express/Node.
Stars: ✭ 957 (+1893.75%)
Mutual labels:  redis, cache

redis-tag-cache

Cache and invalidate records in Redis with tags.

Installation

yarn add redis-tag-cache
# or
npm install redis-tag-cache

Demo Usage

import TagCache from 'redis-tag-cache';

const cache = new TagCache({
  defaultTimeout: 86400 // Expire records after a day (even if they weren't invalidated)
});

/*
 * Cache some records tagged with IDs
 */ 

// Store two posts by the same author
await cache.set(
  'post:id-123',
  { id: 'id-123', title: 'Hello world', author: 'user-123' },
  ['id-123', 'user-123']
);
await cache.set(
  'post:id-234',
  { id: 'id-234', title: 'Hello world again', author: 'user-123' },
  ['id-234', 'user-123']
);
// And a third post by a different author
// and set a custom timeout for it
await cache.set(
  'post:id-345',
  { id: 'id-345', title: 'Hello world again', author: 'user-234' },
  ['id-345', 'user-234'],
  { timeout: 604800 /* Cache for a week */ }
);

/*
 * Retrieve records by their ID
 */

console.log(await cache.get('post:id-234')) // => { id: 'id-234', title: 'Hello world again', author: 'user-123' }

/*
 * Invalidate records by their tags
 */

// Invalidate all records tagged with `user-123`
await cache.invalidate('user-123');
console.log(await cache.get('post:id-123')) // => null
console.log(await cache.get('post:id-234')) // => null
// The third post not tagged with `user-123` is still around!
console.log(await cache.get('post:id-345')) // => { id: 'id-345', title: 'Hello world again', author: 'user-234' }

API

Base class

const cache = new TagCache(options);

options

Options can be an object containing any of the following keys:

  • defaultTimeout: number of seconds until records expire even if not invalidated
  • redis: any ioredis option, this object is directly passed through to new Redis(ioredisOptions)

Example:

const cache = new TagCache({
  defaultTimeout: 86400,
  redis: {
    keyPrefix: 'my-cache', // Recommended: set a keyprefix for all keys stored via the cache
    port: 6379,
    host: 'redis-service.com',
    password: 'password',
  }
});

cache.set(key, value, tags)

Store a record in Redis. Usage:

cache.set(key: string, value: any, tags: Array<string>, options?: Object): Promise<void>

options

Can have one of the following keys:

  • timeout: number of seconds until the record times out, overrides defaultTimeout option

Example

cache.set('some-key', 'some-value', ['some-tag'], { timeout: 123, })cache
  .then(() => console.log('Stored successfully!'))

cache.get(...keys)

Get records from the cache. Usage:

cache.get(...keys: Array<string>): Promise<Array<?value> | ?value>

Example

cache.get('existing-key')
  .then(data => console.log('Got record!', data));

cache.get('not-existing-key')
  .then(data => console.log('data is null', data === null));

cache.get('key-1', 'key-2')
  .then(data => console.log('got multiple keys', data[0], data[1]));

cache.invalidate(...tags)

Invalidate a set of tags and any records associated with them. Usage:

cache.invalidate(tag1: string, tag2: string, ...): Promise<void>

Example

cache.invalidate('some-tag', 'some-other-tag')
  .then(() => console.log('Tags invalidated successfully!'))

Under the hood

Under the hood we store one set for each tag with all its associated keys and your data as a separate record. For example:

cache.set('some-key', 'some-value', ['some-tag']);
cache.set('some-other-key', 'some-other-value', ['some-tag', 'some-other-tag'];

With these two .set calls you'd end up with these records stored in Redis:

  • data:some-key = "some-value"
  • data:some-other-key = "some-other-value"
  • tags:some-tag = ["some-key", "some-other-key"]
  • tags:some-other-tag = ["some-other-key"]

The tradeoff chosen is to keep .get as fast as possible (it's a single redis.get(key), so it couldn't be faster), while making .set a bit slower (since we have to do multiple redis.sets, one for each tags) and .invalidate slow. (since we have to do a redis.get per tag and then a redis.del per record in the tags lists)

PRs implementing this differently under the hood to make .set and/or .invalidate quicker while keeping .get as fast as it is would be appreciated!

License

Licensed under the MIT License, Copyright ©️ 2018 Maximilian Stoiber. See LICENSE.md for more information.

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].