Giter Site home page Giter Site logo

gcache-adapter's Introduction

gcache-adapter

Adapters for package gcache.

Requirements

gf version >= v1.14.0 

Or using the master branch.

Installation

go get -u github.com/gogf/gcache-adapter

Usage

Import

import "github.com/gogf/gcache-adapter/adapter"

Normal Cache

cache := gcache.New()
adapter := adapter.NewRedis(g.Redis())
cache.SetAdapter(adapter)

Change Database Cache From In-Memory To Redis

adapter := adapter.NewRedis(g.Redis())
g.DB().GetCache().SetAdapter(adapter)

gcache-adapter's People

Contributors

gqcn avatar houseme avatar zwytan avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar

gcache-adapter's Issues

GetOrSetFunc 获取结果出错

Internal Error: interface conversion: interface {} is string, not *response.HomeResponse

缓存结构体的话, 再次取出来会变成字符串
image

SetIfNotExist方法需要duration参数大于0才生效

func main() {

	var port int

	var cache = gcache.New()
	adapter := adapter.NewRedis(g.Redis())
	cache.SetAdapter(adapter)

	cache.SetIfNotExist("test_count", 0, 1)
        //redis中 test_count =0 且expire 无限期
        cache.SetIfNotExist("test_count", 0, 0)
       //redis中 test_count 不存在

	flag.IntVar(&port, "p", 8199, "端口号,默认为8199")
	flag.Parse()
	g.Server().SetPort(port)
	g.Server().Run()
}

go version 1.15.5

请问下redis版本的GetOrSetFuncLock怎么没有实现lock功能

// GetOrSetFuncLock retrieves and returns the value of <key>, or sets <key> with result of
// function <f> and returns its result if <key> does not exist in the cache. The key-value
// pair expires after <duration>.
//
// It does not expire if <duration> == 0.
// It does nothing if function <f> returns nil.
//
// Note that the function <f> should be executed within writing mutex lock for concurrent
// safety purpose.
func (c *Redis) GetOrSetFuncLock(ctx context.Context, key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error) {
	return c.GetOrSetFunc(ctx, key, f, duration)
}

代码里直接调用的 GetOrSetFunc 方法,导致并发的时候会穿透到底层

gcache无法设置默认cache的adapt

gf版本v1.14.0
gcache-adapter版本v0.0.2

由于默认Cache对象是私有的,无法设置adapt,代码如下

package gcache

// Default cache object.
var defaultCache = New()

期望:
如gcache.Set可以通过adapt默认使用redis,加入gcache.setAdapt

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.