Commit 95906d78 by 孙龙

up

parent 875c7669
[default_redis_read]
host = 192.168.1.235:6379
password = icDb29mLy2s
max_idle = 50
max_active = 100
idle_timeout = 20
[default_redis_write]
host = 192.168.1.235:6379
password = icDb29mLy2s
max_idle = 50
max_active = 100
idle_timeout = 20
[api_redis_read]
host = 192.168.1.235:6379
password = icDb29mLy2s
max_idle = 50
max_active = 100
idle_timeout = 20
[api_redis_write]
host = 192.168.1.235:6379
password = icDb29mLy2s
max_idle = 50
max_active = 100
idle_timeout = 20
......@@ -11,7 +11,8 @@ var (
func SetUp(path string) (err error) {
//引入多个文件
Cfg, err = ini.LooseLoad(path+"/config.ini", path+"/search.ini", path+"/redis_key.ini", path+"/rabmq_key.ini", path+"/database.ini")
Cfg, err = ini.LooseLoad(path+"/config.ini", path+"/search.ini", path+"/redis_key.ini", path+"/rabmq_key.ini", path+"/database.ini",
path+"/redis_config.ini")
return
}
......
package config
type RedisDatabase struct {
Password string
Host string
Database string
MaxIdle string
MaxActive string
MaxAIdleTimeoutctive string
Prefix string
}
//多数据库配置
func BuildRedisConfgs() (RedisDatabaseMap map[string]RedisDatabase) {
return map[string]RedisDatabase{
"search_read": {
Host: Get("default_redis_read.host").String(),
Password: Get("default_redis_read.password").String(),
MaxIdle: Get("default_redis_read.max_idle").String(),
MaxActive: Get("default_redis_read.max_active").String(),
},
"search_write": {
Host: Get("default_redis_write.host").String(),
Password: Get("default_redis_write.password").String(),
MaxIdle: Get("default_redis_read.max_idle").String(),
MaxActive: Get("default_redis_read.max_active").String(),
},
"spu_read": {
Host: Get("default_redis_read.host").String(),
Password: Get("default_redis_read.password").String(),
MaxIdle: Get("default_redis_read.max_idle").String(),
MaxActive: Get("default_redis_read.max_active").String(),
},
"spu_write": {
Host: Get("default_redis_write.host").String(),
Password: Get("default_redis_write.password").String(),
MaxIdle: Get("default_redis_read.max_idle").String(),
MaxActive: Get("default_redis_read.max_active").String(),
},
"api_read": {
Host: Get("api_redis_read.host").String(),
Password: Get("api_redis_read.password").String(),
MaxIdle: Get("api_redis_read.max_idle").String(),
MaxActive: Get("api_redis_read.max_active").String(),
},
"api_write": {
Host: Get("api_redis_write.host").String(),
Password: Get("api_redis_write.password").String(),
MaxIdle: Get("api_redis_write.max_idle").String(),
MaxActive: Get("api_redis_write.max_active").String(),
},
}
}
......@@ -7,44 +7,35 @@ import (
"time"
)
type IchuntRedisInterface interface {
get()
set()
}
type IchuntRedis struct {
RedisList map[string]*redis.Pool
Current *redis.Pool
}
func(this *IchuntRedis) Connection(connection string,host string,passwd string) (*redis.Pool,error){
redisHost :=config.Get(host).String()
redisPasswd := config.Get(passwd).String()
redisPoll, err := getConn(redisHost, redisPasswd)
if err != nil {
return nil,err
}
return redisPoll,nil
func(this *IchuntRedis) Connection(connection string) (*IchuntRedis){
this.Current = this.RedisList[connection]
return this
}
var writeConn, readConn *redis.Pool
func Setup() (err error) {
writeHost := config.Get("redis.write_host").String()
readHost := config.Get("redis.read_host").String()
writePassword := config.Get("redis.write_password").String()
readPassword := config.Get("redis.read_password").String()
writeConn, err = getConn(writeHost, writePassword)
if err != nil {
return
ichuntRedis := &IchuntRedis{}
ichuntRedis.RedisList = make(map[string]*redis.Pool,0)
RedisDatabaseMap := config.BuildRedisConfgs()
for redisKey,redisConfig := range RedisDatabaseMap{
ichuntRedis.RedisList[redisKey],err = getConn(redisConfig.Host, redisConfig.Password)
if err != nil{
panic(err)
}
readConn, err = getConn(readHost, readPassword)
if err != nil {
return
}
return nil
}
func getConn(writeHost, password string) (pool *redis.Pool, err error) {
maxIdle, _ := config.Get("redis.max_idle").Int()
maxActive, _ := config.Get("redis.max_active").Int()
......@@ -74,8 +65,8 @@ func getConn(writeHost, password string) (pool *redis.Pool, err error) {
//最基础的键值操作
func Set(key string, data interface{}) error {
conn := writeConn.Get()
func (this *IchuntRedis) Set(key string, data interface{}) error {
conn := this.Current.Get()
defer conn.Close()
value, err := json.Marshal(data)
......@@ -92,8 +83,8 @@ func Set(key string, data interface{}) error {
}
//Redis Setnx(SET if Not eXists) 命令在指定的 key 不存在时,为 key 设置指定的值。
func Setnx(key string, data interface{}) error {
conn := writeConn.Get()
func(this *IchuntRedis) Setnx(key string, data interface{}) error {
conn := this.Current.Get()
defer conn.Close()
value, err := json.Marshal(data)
......@@ -109,8 +100,8 @@ func Setnx(key string, data interface{}) error {
return nil
}
func Exists(key string) bool {
conn := readConn.Get()
func(this *IchuntRedis) Exists(key string) bool {
conn := this.Current.Get()
defer conn.Close()
exists, err := redis.Bool(conn.Do("EXISTS", key))
......@@ -121,8 +112,8 @@ func Exists(key string) bool {
return exists
}
func Get(key string) (interface{}, error) {
conn := readConn.Get()
func(this *IchuntRedis) Get(key string) (interface{}, error) {
conn := this.Current.Get()
defer conn.Close()
reply, err := conn.Do("GET", key)
......@@ -133,8 +124,8 @@ func Get(key string) (interface{}, error) {
return reply, nil
}
func Delete(key string) (bool, error) {
conn := writeConn.Get()
func(this *IchuntRedis) Delete(key string) (bool, error) {
conn := this.Current.Get()
defer conn.Close()
return redis.Bool(conn.Do("DEL", key))
......@@ -142,8 +133,8 @@ func Delete(key string) (bool, error) {
//哈希操作
func HSet(key string, k interface{}, data interface{}) error {
conn := writeConn.Get()
func(this *IchuntRedis) HSet(key string, k interface{}, data interface{}) error {
conn := this.Current.Get()
defer conn.Close()
value, err := json.Marshal(data)
......@@ -159,8 +150,8 @@ func HSet(key string, k interface{}, data interface{}) error {
return nil
}
func HGet(key string, k interface{}) (interface{}, error) {
conn := readConn.Get()
func(this *IchuntRedis) HGet(key string, k interface{}) (interface{}, error) {
conn := this.Current.Get()
defer conn.Close()
reply, err := conn.Do("HGET", key, k)
......@@ -171,8 +162,8 @@ func HGet(key string, k interface{}) (interface{}, error) {
return reply, nil
}
func HDelete(key string, k interface{}) (bool, error) {
conn := writeConn.Get()
func(this *IchuntRedis) HDelete(key string, k interface{}) (bool, error) {
conn := this.Current.Get()
defer conn.Close()
return redis.Bool(conn.Do("HDEL", key, k))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment