完成gredis包开发

This commit is contained in:
John
2018-04-18 18:05:46 +08:00
parent 91af060e8a
commit 9952761761
8 changed files with 180 additions and 10 deletions

View File

@ -4,7 +4,7 @@
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.
// 数据库ORM操作.
// 数据库ORM.
package gdb
import (

106
g/database/gredis/gredis.go Normal file
View File

@ -0,0 +1,106 @@
// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.
// Redis客户端.
package gredis
import (
"time"
"github.com/gomodule/redigo/redis"
)
const (
gDEFAULT_POOL_MAX_IDLE = 1
gDEFAULT_POOL_MAX_ACTIVE = 10
gDEFAULT_POOL_IDLE_TIMEOUT = 180 * time.Second
gDEFAULT_POOL_MAX_LIFE_TIME = 60 * time.Second
)
// Redis客户端
type Redis struct {
address string
db interface{}
pool *redis.Pool
}
// Redis链接池统计信息
type PoolStats struct {
redis.PoolStats
}
// 创建redis操作对象
// address参数格式 host:port
func New(address string, db ... interface{}) *Redis {
r := &Redis{}
r.address = address
if len(db) > 0 {
r.db = db[0]
}
r.pool = &redis.Pool {
MaxIdle : gDEFAULT_POOL_MAX_IDLE,
MaxActive : gDEFAULT_POOL_MAX_ACTIVE,
IdleTimeout : gDEFAULT_POOL_IDLE_TIMEOUT,
MaxConnLifetime : gDEFAULT_POOL_MAX_LIFE_TIME,
Dial : r.dialFunc(),
}
return r
}
// 关闭链接
func (r *Redis) Close() error {
return r.pool.Close()
}
// 设置属性 - MaxIdle
func (r *Redis) SetMaxIdle(value int) {
r.pool.MaxIdle = value
}
// 设置属性 - MaxActive
func (r *Redis) SetMaxActive(value int) {
r.pool.MaxActive = value
}
// 设置属性 - IdleTimeout
func (r *Redis) SetIdleTimeout(value time.Duration) {
r.pool.IdleTimeout = value
}
// 设置属性 - MaxConnLifetime
func (r *Redis) SetMaxConnLifetime(value time.Duration) {
r.pool.MaxConnLifetime = value
}
// 获取当前连接池统计信息
func (r *Redis) Stats() *PoolStats {
return &PoolStats{r.pool.Stats()}
}
// 执行命令 - Do
func (r *Redis) Do(command string, args ...interface{}) (interface{}, error) {
c := r.pool.Get()
defer c.Close()
return c.Do(command, args...)
}
// 执行命令 - Send
func (r *Redis) Send(command string, args ...interface{}) error {
c := r.pool.Get()
defer c.Close()
return c.Send(command, args...)
}
// 构造链接redis方法
func (r *Redis) dialFunc() func()(redis.Conn, error) {
return func() (redis.Conn, error) {
c, err := redis.Dial("tcp", r.address)
if err != nil {
return nil, err
}
c.Do("SELECT", r.db)
return c, nil
}
}

View File

@ -17,12 +17,16 @@ import (
"gitee.com/johng/gf/g/os/gfile"
"gitee.com/johng/gf/g/database/gdb"
"gitee.com/johng/gf/g/container/gmap"
"gitee.com/johng/gf/g/database/gredis"
"strings"
"gitee.com/johng/gf/g/util/gconv"
)
const (
gFRAME_CORE_COMPONENT_NAME_VIEW = "gf.core.component.view"
gFRAME_CORE_COMPONENT_NAME_CONFIG = "gf.core.component.config"
gFRAME_CORE_COMPONENT_NAME_DATABASE = "gf.core.component.database"
gFRAME_CORE_COMPONENT_NAME_REDIS = "gf.core.component.redis"
)
// 单例对象存储器
@ -49,10 +53,10 @@ func SetConfig(v *gcfg.Config) {
}
// 自定义框架核心组件Database
func SetDatabase(v *gdb.Db, names...string) {
func SetDatabase(v *gdb.Db, name...string) {
dbCacheKey := gFRAME_CORE_COMPONENT_NAME_DATABASE
if len(names) > 0 {
dbCacheKey += names[0]
if len(name) > 0 {
dbCacheKey += name[0]
}
instances.Set(dbCacheKey, v)
}
@ -99,10 +103,10 @@ func Config() *gcfg.Config {
}
// 核心对象Database
func Database(names...string) *gdb.Db {
func Database(name...string) *gdb.Db {
dbCacheKey := gFRAME_CORE_COMPONENT_NAME_DATABASE
if len(names) > 0 {
dbCacheKey += names[0]
if len(name) > 0 {
dbCacheKey += name[0]
}
result := Get(dbCacheKey)
if result != nil {
@ -150,7 +154,7 @@ func Database(names...string) *gdb.Db {
}
}
if db, err := gdb.Instance(names...); err == nil {
if db, err := gdb.Instance(name...); err == nil {
Set(dbCacheKey, db)
return db
} else {
@ -159,4 +163,34 @@ func Database(names...string) *gdb.Db {
}
}
return nil
}
// 核心对象Redis
func Redis(name...string) *gredis.Redis {
group := "default"
redisCacheKey := gFRAME_CORE_COMPONENT_NAME_REDIS
if len(name) > 0 {
group = name[0]
redisCacheKey += name[0]
}
result := Get(redisCacheKey)
if result != nil {
return result.(*gredis.Redis)
} else {
config := Config()
if config == nil {
return nil
}
if m := config.GetMap("redis"); m != nil {
if v, ok := m[group]; ok {
array := strings.Split(gconv.String(v), ",")
if len(array) > 1 {
redis := gredis.New(array[0], array[1])
Set(redisCacheKey, redis)
return redis
}
}
}
}
return nil
}

View File

@ -0,0 +1,15 @@
package main
import (
"fmt"
"gitee.com/johng/gf/g/database/gredis"
)
func main() {
redis := gredis.New("127.0.0.1:6379", 1)
fmt.Println(redis.Do("SET", "k1", "v1"))
fmt.Println(redis.Do("SET", "k2", "v3"))
fmt.Println(redis.Do("GET", "k2"))
fmt.Println(redis.Stats())
}

View File

@ -25,5 +25,9 @@
"priority" : "1"
}
]
},
"redis" : {
"disk" : "127.0.0.1:6379,0",
"cache" : "127.0.0.1:6379,1"
}
}

View File

@ -22,3 +22,7 @@ viewpath = "/home/www/templates/"
role = "master"
charset = "utf8"
priority = "1"
# Redis数据库配置
[redis]
disk = "127.0.0.1:6379,0"
cache = "127.0.0.1:6379,1"

View File

@ -27,4 +27,9 @@
<priority>1</priority>
</default>
</database>
<!-- Redis数据库配置 -->
<redis>
<disk>127.0.0.1:6379,0</disk>
<cache>127.0.0.1:6379,1</cache>
</redis>
</config>

View File

@ -21,5 +21,7 @@ database:
role: master
charset: utf-8
priority: 1
# Redis数据库配置
redis:
disk: 127.0.0.1:6379,0
cache: 127.0.0.1:6379,1