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 ( ...@@ -11,7 +11,8 @@ var (
func SetUp(path string) (err error) { 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 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 ( ...@@ -7,44 +7,35 @@ import (
"time" "time"
) )
type IchuntRedisInterface interface {
get()
set()
}
type IchuntRedis struct { type IchuntRedis struct {
RedisList map[string]*redis.Pool
Current *redis.Pool
} }
func(this *IchuntRedis) Connection(connection string,host string,passwd string) (*redis.Pool,error){ func(this *IchuntRedis) Connection(connection string) (*IchuntRedis){
redisHost :=config.Get(host).String() this.Current = this.RedisList[connection]
redisPasswd := config.Get(passwd).String() return this
redisPoll, err := getConn(redisHost, redisPasswd)
if err != nil {
return nil,err
}
return redisPoll,nil
} }
var writeConn, readConn *redis.Pool var writeConn, readConn *redis.Pool
func Setup() (err error) { func Setup() (err error) {
writeHost := config.Get("redis.write_host").String() ichuntRedis := &IchuntRedis{}
readHost := config.Get("redis.read_host").String() ichuntRedis.RedisList = make(map[string]*redis.Pool,0)
writePassword := config.Get("redis.write_password").String() RedisDatabaseMap := config.BuildRedisConfgs()
readPassword := config.Get("redis.read_password").String() for redisKey,redisConfig := range RedisDatabaseMap{
writeConn, err = getConn(writeHost, writePassword) ichuntRedis.RedisList[redisKey],err = getConn(redisConfig.Host, redisConfig.Password)
if err != nil { if err != nil{
return panic(err)
} }
readConn, err = getConn(readHost, readPassword)
if err != nil {
return
} }
return nil return nil
} }
func getConn(writeHost, password string) (pool *redis.Pool, err error) { func getConn(writeHost, password string) (pool *redis.Pool, err error) {
maxIdle, _ := config.Get("redis.max_idle").Int() maxIdle, _ := config.Get("redis.max_idle").Int()
maxActive, _ := config.Get("redis.max_active").Int() maxActive, _ := config.Get("redis.max_active").Int()
...@@ -74,8 +65,8 @@ func getConn(writeHost, password string) (pool *redis.Pool, err error) { ...@@ -74,8 +65,8 @@ func getConn(writeHost, password string) (pool *redis.Pool, err error) {
//最基础的键值操作 //最基础的键值操作
func Set(key string, data interface{}) error { func (this *IchuntRedis) Set(key string, data interface{}) error {
conn := writeConn.Get() conn := this.Current.Get()
defer conn.Close() defer conn.Close()
value, err := json.Marshal(data) value, err := json.Marshal(data)
...@@ -92,8 +83,8 @@ func Set(key string, data interface{}) error { ...@@ -92,8 +83,8 @@ func Set(key string, data interface{}) error {
} }
//Redis Setnx(SET if Not eXists) 命令在指定的 key 不存在时,为 key 设置指定的值。 //Redis Setnx(SET if Not eXists) 命令在指定的 key 不存在时,为 key 设置指定的值。
func Setnx(key string, data interface{}) error { func(this *IchuntRedis) Setnx(key string, data interface{}) error {
conn := writeConn.Get() conn := this.Current.Get()
defer conn.Close() defer conn.Close()
value, err := json.Marshal(data) value, err := json.Marshal(data)
...@@ -109,8 +100,8 @@ func Setnx(key string, data interface{}) error { ...@@ -109,8 +100,8 @@ func Setnx(key string, data interface{}) error {
return nil return nil
} }
func Exists(key string) bool { func(this *IchuntRedis) Exists(key string) bool {
conn := readConn.Get() conn := this.Current.Get()
defer conn.Close() defer conn.Close()
exists, err := redis.Bool(conn.Do("EXISTS", key)) exists, err := redis.Bool(conn.Do("EXISTS", key))
...@@ -121,8 +112,8 @@ func Exists(key string) bool { ...@@ -121,8 +112,8 @@ func Exists(key string) bool {
return exists return exists
} }
func Get(key string) (interface{}, error) { func(this *IchuntRedis) Get(key string) (interface{}, error) {
conn := readConn.Get() conn := this.Current.Get()
defer conn.Close() defer conn.Close()
reply, err := conn.Do("GET", key) reply, err := conn.Do("GET", key)
...@@ -133,8 +124,8 @@ func Get(key string) (interface{}, error) { ...@@ -133,8 +124,8 @@ func Get(key string) (interface{}, error) {
return reply, nil return reply, nil
} }
func Delete(key string) (bool, error) { func(this *IchuntRedis) Delete(key string) (bool, error) {
conn := writeConn.Get() conn := this.Current.Get()
defer conn.Close() defer conn.Close()
return redis.Bool(conn.Do("DEL", key)) return redis.Bool(conn.Do("DEL", key))
...@@ -142,8 +133,8 @@ func Delete(key string) (bool, error) { ...@@ -142,8 +133,8 @@ func Delete(key string) (bool, error) {
//哈希操作 //哈希操作
func HSet(key string, k interface{}, data interface{}) error { func(this *IchuntRedis) HSet(key string, k interface{}, data interface{}) error {
conn := writeConn.Get() conn := this.Current.Get()
defer conn.Close() defer conn.Close()
value, err := json.Marshal(data) value, err := json.Marshal(data)
...@@ -159,8 +150,8 @@ func HSet(key string, k interface{}, data interface{}) error { ...@@ -159,8 +150,8 @@ func HSet(key string, k interface{}, data interface{}) error {
return nil return nil
} }
func HGet(key string, k interface{}) (interface{}, error) { func(this *IchuntRedis) HGet(key string, k interface{}) (interface{}, error) {
conn := readConn.Get() conn := this.Current.Get()
defer conn.Close() defer conn.Close()
reply, err := conn.Do("HGET", key, k) reply, err := conn.Do("HGET", key, k)
...@@ -171,8 +162,8 @@ func HGet(key string, k interface{}) (interface{}, error) { ...@@ -171,8 +162,8 @@ func HGet(key string, k interface{}) (interface{}, error) {
return reply, nil return reply, nil
} }
func HDelete(key string, k interface{}) (bool, error) { func(this *IchuntRedis) HDelete(key string, k interface{}) (bool, error) {
conn := writeConn.Get() conn := this.Current.Get()
defer conn.Close() defer conn.Close()
return redis.Bool(conn.Do("HDEL", key, k)) 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