diff --git a/g/database/gdb/gdb.go b/g/database/gdb/gdb.go
index 26b201b9e..8d5322248 100644
--- a/g/database/gdb/gdb.go
+++ b/g/database/gdb/gdb.go
@@ -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 (
diff --git a/g/database/gredis/gredis.go b/g/database/gredis/gredis.go
new file mode 100644
index 000000000..48167b66f
--- /dev/null
+++ b/g/database/gredis/gredis.go
@@ -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
+ }
+}
\ No newline at end of file
diff --git a/g/frame/gins/gins.go b/g/frame/gins/gins.go
index 9d9f42e12..e07f79a1e 100644
--- a/g/frame/gins/gins.go
+++ b/g/frame/gins/gins.go
@@ -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
}
\ No newline at end of file
diff --git a/geg/database/redis/gredis.go b/geg/database/redis/gredis.go
new file mode 100644
index 000000000..d98e4ee4e
--- /dev/null
+++ b/geg/database/redis/gredis.go
@@ -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())
+}
+
diff --git a/geg/frame/config.json b/geg/frame/config.json
index 433dcf4a9..c10a4cdb6 100644
--- a/geg/frame/config.json
+++ b/geg/frame/config.json
@@ -25,5 +25,9 @@
"priority" : "1"
}
]
+ },
+ "redis" : {
+ "disk" : "127.0.0.1:6379,0",
+ "cache" : "127.0.0.1:6379,1"
}
}
\ No newline at end of file
diff --git a/geg/frame/config.toml b/geg/frame/config.toml
index 92ace1c86..d58ffea7c 100644
--- a/geg/frame/config.toml
+++ b/geg/frame/config.toml
@@ -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"
\ No newline at end of file
diff --git a/geg/frame/config.xml b/geg/frame/config.xml
index fa269ad68..8fb664b30 100644
--- a/geg/frame/config.xml
+++ b/geg/frame/config.xml
@@ -27,4 +27,9 @@
1
+
+
+ 127.0.0.1:6379,0
+ 127.0.0.1:6379,1
+
\ No newline at end of file
diff --git a/geg/frame/config.yml b/geg/frame/config.yml
index 9d5b34cc2..406c0d8ab 100644
--- a/geg/frame/config.yml
+++ b/geg/frame/config.yml
@@ -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