Initial commit
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis"
|
||||
)
|
||||
|
||||
//key: {name:value, name:value, name:value}
|
||||
|
||||
type Hash struct {
|
||||
key string
|
||||
client *redis.Client
|
||||
}
|
||||
|
||||
func NewHash(client *redis.Client, key string) *Hash {
|
||||
return &Hash{
|
||||
key: key,
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Hash) Key() string {
|
||||
return s.key
|
||||
}
|
||||
|
||||
// Expire 设置集合过期时间
|
||||
func (s *Hash) Expire(expiration time.Duration) (bool, error) {
|
||||
return s.client.Expire(s.key, expiration).Result()
|
||||
}
|
||||
|
||||
// Delete 删除一个或多个 Name
|
||||
func (s *Hash) Delete(names ...string) (int64, error) {
|
||||
return s.client.HDel(s.key, names...).Result()
|
||||
}
|
||||
|
||||
// Exists 检测Name是否在哈希表中
|
||||
func (s *Hash) Exists(name string) (bool, error) {
|
||||
return s.client.HExists(s.key, name).Result()
|
||||
}
|
||||
|
||||
// NameValues 获取所有哈希表数据
|
||||
func (s *Hash) NameValues() (map[string]string, error) {
|
||||
return s.client.HGetAll(s.key).Result()
|
||||
}
|
||||
|
||||
// Names 获取所有哈希表的 Name
|
||||
func (s *Hash) Names() ([]string, error) {
|
||||
return s.client.HKeys(s.key).Result()
|
||||
}
|
||||
|
||||
// Values 获取所有哈希表的 Total
|
||||
func (s *Hash) Values() ([]string, error) {
|
||||
return s.client.HVals(s.key).Result()
|
||||
}
|
||||
|
||||
// SetNX 当name不存在时才向哈希表中设置一对Name:Total
|
||||
func (s *Hash) SetNX(name string, value interface{}) (bool, error) {
|
||||
return s.client.HSetNX(s.key, name, value).Result()
|
||||
}
|
||||
|
||||
// Set 向哈希表中设置一对Name:Total
|
||||
func (s *Hash) Set(name string, value interface{}) (bool, error) {
|
||||
return s.client.HSet(s.key, name, value).Result()
|
||||
}
|
||||
|
||||
// Get 获取哈希表中Name对应的Value
|
||||
func (s *Hash) Get(name string) (string, error) {
|
||||
return s.client.HGet(s.key, name).Result()
|
||||
}
|
||||
|
||||
// IncInt 返回累加之后的新值
|
||||
func (s *Hash) IncInt(name string, value int64) (int64, error) {
|
||||
return s.client.HIncrBy(s.key, name, value).Result()
|
||||
}
|
||||
|
||||
// IncFloat 返回累加之后的新值
|
||||
func (s *Hash) IncFloat(name string, value float64) (float64, error) {
|
||||
return s.client.HIncrByFloat(s.key, name, value).Result()
|
||||
}
|
||||
|
||||
// Len 返回哈希表的大小
|
||||
func (s *Hash) Len() (int64, error) {
|
||||
return s.client.HLen(s.key).Result()
|
||||
}
|
||||
|
||||
// Scan 扫描哈希表数据
|
||||
func (s *Hash) Scan(match string, piece int64, callback func(name, value string)) error {
|
||||
proc := func(l []string) {
|
||||
for i := 0; i+1 < len(l); i += 2 {
|
||||
callback(l[i], l[i+1])
|
||||
}
|
||||
}
|
||||
lst, cur, err := s.client.HScan(s.key, 0, match, piece).Result()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
proc(lst)
|
||||
for cur > 0 {
|
||||
lst, cur, err = s.client.HScan(s.key, cur, match, piece).Result()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
proc(lst)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user