Coder Social home page Coder Social logo

cacheya's Introduction

cacheya

介绍

Golang 缓存封装,支持Context、内存、Redis多种缓存适配,实现便捷的 Cache-Aside 操作

基本使用

type TestObject struct {
    Id   string
    A    float32
    B    uint32
}


ctx := context.Background()

// 初始化:Redis缓存
opt := &redis.Options{
    Addr:     "127.0.0.1:6379",
}
client := redis.NewClient(opt)

adapter := goredis.NewRedisCache(client)
manager := NewCacheManager[TestObject, string]("TEST_OBJECT", adapter)   // 范型中第一个指定缓存的对象,第二个指定id的类型

// 初始化:内存缓存
client, _ := ristretto.NewCache(
    &ristretto.Config{
        NumCounters: 1e7,  // number of keys to track frequency of (10M).
        MaxCost:     10e7, // maximum cost of cache (100M).
        BufferItems: 64,   // number of keys per Get buf
    },
)
adapter := memory.NewRistrettoCache(client)
manager := NewCacheManager[TestObject, string]("TEST_OBJECT", adapter)

// 初始化:Context缓存
// 注意,使用时需要在服务内ctx生命周期开始时(比如grpc的中间件中),使用WithCtxCache对ctx开启缓存,否则无效
adapter := ctxcache.NewContextCache()
manager := NewCacheManager[TestObject, string]("TEST_OBJECT", adapter)

// 单数据操作
r, c, err := manager.Get(ctx, "1")                 // Get
err = manager.Set(ctx, "2", &TestObject{Id: "2"})  // Set
err = manager.Del(ctx, "1")                        // Del
r, err = manager.Load(                             // Cache-Aside
    ctx, "1", func(k string) (*TestObject, error) {
        // 这里实现回源的代码
    },
)

r, err = manager.Loadv(                            // Cache-Aside,与Load的区别是,这个回源和最终返回都是值,适用于缓存基本类型string、bool的这种场景
    ctx, "1", func(k string) (TestObject, error) {
    // 这里实现回源的代码
    },
)


// 批量操作
r, err := manager.MGet(ctx, []string{"1", "2"})    // MGet
err = manager.MSet(ctx, kv)                        // MSet
err = manager.MDel(ctx, []string{"1", "2"})        // MDel
r, err = manager.MLoad(                            // Multi Cache-Aside
    ctx, []string{"1", "2"}, func(ks []string) (map[string]*TestObject, error) {
        // 这里实现回源的代码
    },
)

r, err = manager.MLoadv(                            // Multi Cache-Aside,与MLoad的区别是,这个回源和最终返回都是值,适用于缓存基本类型string、bool的这种场景
    ctx, []string{"1", "2"}, func(ks []string) (map[string]TestObject, error) {
    // 这里实现回源的代码
    },
)

cacheya's People

Contributors

kongxinchi avatar

Watchers

 avatar

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.