Git Product home page Git Product logo

Comments (5)

askuy avatar askuy commented on August 22, 2024

使用场景:

在本地使用的单个Redis:
redisStub := redis.StdRedisConfig("myredis").Build()

在正式环境使用的是Redis集群:
redisStub := redis.StdRedisClusterConfig("myredis").Build()

这样就产生一个问题,在代码里使用Redis的地方都需要判断是本地环境还是线上正式环境。
在框架中是否可以进一步封装,对于使用者不用管是什么环境,只需要传一个参数来确定是否为Redis集群?或者有更好的方案?

这个可以规定,redis cluster和普通redis实现了一个redis interface。

然后你通过运行环境,在代码层面将不同的redis实例赋值给定义为interface的Redis Stub,如下所示

var RedisStub RedisInterface
if env =="prod" {
    RedisStub = redis.StdRedisClusterConfig("myredis").Build()
}else {
    RedisStub = redis.StdRedisConfig("myredis").Build()
}

type RedisInterface interface {
    Get()
    Set()
    ...
}

我们会看一下,普通redis和redis cluster的方法有没有统一起来。你也可以按我们方法,先自己试一下。

from jupiter.

cw2018 avatar cw2018 commented on August 22, 2024

ok

from jupiter.

OhBonsai avatar OhBonsai commented on August 22, 2024

Hi, 有人认领了这个Issue, 如果没有 我想Pick~

from jupiter.

askuy avatar askuy commented on August 22, 2024

Hi, 有人认领了这个Issue, 如果没有 我想Pick~
非常欢迎

from jupiter.

OhBonsai avatar OhBonsai commented on August 22, 2024

@askuy PTAL

from jupiter.

Related Issues (20)

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.