GithubHelp home page GithubHelp logo

dtm-labs / rockscache Goto Github PK

View Code? Open in Web Editor NEW
520.0 20.0 70.0 85 KB

The First Redis Cache Library To Ensure Eventual Consistency And Strong Consistency With DB.

Home Page: http://d.dtm.pub/app/cache.html

License: BSD 3-Clause "New" or "Revised" License

Go 99.30% Shell 0.70%
cache consistency distributed redis

rockscache's Introduction

license Build Status codecov Go Report Card Go Reference

English | 简体中文

RocksCache

The first Redis cache library to ensure eventual consistency and strong consistency with DB.

Features

  • Eventual Consistency: ensures eventual consistency of cache even in extreme cases
  • Strong consistency: provides strong consistent access to applications
  • Anti-breakdown: a better solution for cache breakdown
  • Anti-penetration
  • Anti-avalanche
  • Batch Query

Usage

This cache repository uses the most common update DB and then delete cache cache management policy

Read cache

import "github.com/dtm-labs/rockscache"

// new a client for rockscache using the default options
rc := rockscache.NewClient(redisClient, NewDefaultOptions())

// use Fetch to fetch data
// 1. the first parameter is the key of the data
// 2. the second parameter is the data expiration time
// 3. the third parameter is the data fetch function which is called when the cache does not exist
v, err := rc.Fetch("key1", 300 * time.Second, func()(string, error) {
  // fetch data from database or other sources
  return "value1", nil
})

Delete the cache

rc.TagAsDeleted(key)

Batch usage

Batch read cache

import "github.com/dtm-labs/rockscache"

// new a client for rockscache using the default options
rc := rockscache.NewClient(redisClient, NewDefaultOptions())

// use FetchBatch to fetch data
// 1. the first parameter is the keys list of the data
// 2. the second parameter is the data expiration time
// 3. the third parameter is the batch data fetch function which is called when the cache does not exist
// the parameter of the batch data fetch function is the index list of those keys
// missing in cache, which can be used to form a batch query for missing data.
// the return value of the batch data fetch function is a map, with key of the
// index and value of the corresponding data in form of string
v, err := rc.FetchBatch([]string{"key1", "key2", "key3"}, 300, func(idxs []int) (map[int]string, error) {
    // fetch data from database or other sources
    values := make(map[int]string)
    for _, i := range idxs {
        values[i] = fmt.Sprintf("value%d", i)
    }
    return values, nil
})

Batch delete cache

rc.TagAsDeletedBatch(keys)

Eventual consistency

With the introduction of caching, consistency problems in a distributed system show up, as the data is stored in two places at the same time: the database and Redis. For background on this consistency problem, and an introduction to popular Redis caching solutions, see.

But all the caching solutions we've seen so far, without introducing versioning at the application level, fail to address the following data inconsistency scenario.

cache-version-problem

Even if you use lock to do the updating, there are still corner cases that can cause inconsistency.

redis cache inconsistency

Solution

This project brings you a brand new solution that guarantee data consistency between the cache and the database, without introducing version. This solution is the first of its kind and has been patented and is now open sourced for everyone to use.

When the developer calls Fetch when reading the data, and makes sure to call TagAsDeleted after updating the database, then the cache can guarentee the eventual consistency. When step 5 in the diagram above is writing to v1, the write in this solution will eventually be ignored.

For a full runnable example, see dtm-cases/cache

Strongly consistent access

If your application needs to use caching and requires strong consistency rather than eventual consistency, then this can be supported by turning on the option StrongConsisteny, with the access method remaining the same

rc.Options.StrongConsisteny = true

Refer to cache consistency for detailed principles and dtm-cases/cache for examples

Downgrading and strong consistency

The library supports downgrading. The downgrade switch is divided into

  • DisableCacheRead: turns off cache reads, default false; if on, then Fetch does not read from the cache, but calls fn directly to fetch the data
  • DisableCacheDelete: disables cache delete, default false; if on, then TagAsDeleted does nothing and returns directly

When Redis has a problem and needs to be downgraded, you can control this with these two switches. If you need to maintain strong consistent access even during a downgrade, rockscache also supports

Refer to cache-consistency for detailed principles and dtm-cases/cache for examples

Anti-Breakdown

The use of cache through this library comes with an anti-breakdown feature. On the one hand Fetch will use singleflight within the process to avoid multiple requests being sent to Redis within a process, and on the other hand distributed locks will be used in the Redis layer to avoid multiple requests being sent to the DB from multiple processes at the same time, ensuring that only one data query request ends up at the DB.

The project's anti-breakdown provides a faster response time when hot cached data is deleted. If a hot cache data takes 3s to compute, a normal anti-breakdown solution would cause all requests for this hot data to wait 3s for this time, whereas this project's solution returns it immediately.

Anti-Penetration

The use of caching through this library comes with anti-penetration features. When fn in Fetch returns an empty string, this is considered an empty result and the expiry time is set to EmptyExpire in the rockscache option.

EmptyExpire defaults to 60s, if set to 0 then anti-penetration is turned off and no empty results are saved

Anti-Avalanche

The cache is used with this library and comes with an anti-avalanche. RandomExpireAdjustment in rockscache defaults to 0.1, if set to an expiry time of 600 then the expiry time will be set to a random number in the middle of 540s - 600s to avoid data expiring at the same time

Contact us

Chat Group

Join the chat via https://discord.gg/dV9jS5Rb33.

Give a star! ⭐

If you think this project is interesting, or helpful to you, please give a star!

rockscache's People

Contributors

exuan avatar fgadvancer avatar supermigo avatar wangchuxiao-dev avatar wqyjh avatar yedf2 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rockscache's Issues

文中的demo会报错

func main() {
client := redis.NewClient(&redis.Options{
Network: "tcp",
Addr: "127.0.0.1:6379",
DB: 0,
})

rc := rockscache.NewClient(client, rockscache.NewDefaultOptions())
v, err := rc.Fetch("key2", 400, func() (string, error) {
	return "value2", nil
})
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println(v)

}
key2的值为"aaa",获取时报下面的错误
ERR Error running script (call to f_a307994a908d94d53a2b772ddc6cf08109e0a828): @user_script:2: WRONGTYPE Operation against a key holding the wrong kind of value

[Enhancement] Options 支持配置统一缓存前缀

目前 options 中没有字段可以配置统一前缀,如果 Redis 服务器是多场景使用时,我必须要在每次设置缓存时,自定义 key 加统一前缀,如果可以设置统一前缀,可以统一管理,这对于使用更加友好

A call to Fetch after TagAsDeleted returns an old value

client := redis.NewClient(&redis.Options{
		Addr:         "localhost:6379",
		Password:     "",
		DB:           0, // use default DB
	})
rc := rockscache.NewClient(client, rockscache.NewDefaultOptions())
rc.Fetch("test1", 60 * time.Second, func () (string, error) {
return "test1", nil
})
rc.TagAsDeleted("test1")
value, _ := rc.Fetch("test1", 60 * time.Second, func () (string, error) {
return "test2", nil
})
fmt.Println("value:", value)  // except return "test2", but got "test1"

可以添加一个是否缓存的条件么?

我想根据查询结果来判断是否缓存,请问可以添加一个是否缓存的条件么?
v, err := rc.Fetch("key1", 300 * time.Second, func()(string, bool,error) {
// fetch data from database or other sources
notneedcache=false
return "value1",notneedcache, nil
})

rockscache能否支持go-redis v9版本

rockscache的go.mod中还引用v8版本的go-redis,但是我们项目已经使用v9的go-redis了,初始化rockscache的client会报错:
cannot use rdb (variable of type *"github.com/redis/go-redis/v9".Client) as "github.com/go-redis/redis/v8".UniversalClient value in argument to rockscache.NewClient: *"github.com/redis/go-redis/v9".Client does not implement "github.com/go-redis/redis/v8".UniversalClient (wrong type for method AddHook)
have AddHook("github.com/redis/go-redis/v9".Hook)
want AddHook("github.com/go-redis/redis/v8".Hook)

是否可以支持下v9版本的go-redis?

About the lockuntil is expired, and the data is also expired when data is not empty?

Why think that when the data is not empty, the lock expires and the data also expires?Why not use the TagAsDeleted function as the only trigger for cache updates?

I saw the code about default lockexpire time is 3s.Does it mean that an asynchronous update will be triggered every 3s, regardless of whether the data in the database is updated or not?

emo一下

延时锁的方式还是有很多人能想出来的,这个也用得上专利嘛。。。我很早前还在b站某缓存一致性介绍视频下评论过类似的办法呢。只是现在很多技术人员不愿意思考罢了,都是八股文害的。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.