Commit 49e72ff6 by yemin

no message

parent d93a2fb4
...@@ -81,7 +81,7 @@ func Get(key string) []byte { ...@@ -81,7 +81,7 @@ func Get(key string) []byte {
func Set(k, v string) error { func Set(k, v string) error {
e := getDb().Set(k, v, 0).Err() e := getDb().Set(k, v, 0).Err()
if e==nil { if e == nil {
log.Debug(fmt.Sprintf("设置缓存成功:%s %s", k, v)) log.Debug(fmt.Sprintf("设置缓存成功:%s %s", k, v))
} else { } else {
log.Debug(fmt.Sprintf("设置缓存失败:%s %s %s", k, v, e)) log.Debug(fmt.Sprintf("设置缓存失败:%s %s %s", k, v, e))
...@@ -90,8 +90,8 @@ func Set(k, v string) error { ...@@ -90,8 +90,8 @@ func Set(k, v string) error {
} }
func SetNx(k, v string, timeoutSecond int) error { func SetNx(k, v string, timeoutSecond int) error {
e := getDb().Set(k, v, time.Duration(timeoutSecond) * time.Second).Err() e := getDb().Set(k, v, time.Duration(timeoutSecond)*time.Second).Err()
if e==nil { if e == nil {
log.Debug(fmt.Sprintf("设置缓存成功:%s %s", k, v)) log.Debug(fmt.Sprintf("设置缓存成功:%s %s", k, v))
} else { } else {
log.Debug(fmt.Sprintf("设置缓存失败:%s %s %s", k, v, e)) log.Debug(fmt.Sprintf("设置缓存失败:%s %s %s", k, v, e))
...@@ -99,7 +99,7 @@ func SetNx(k, v string, timeoutSecond int) error { ...@@ -99,7 +99,7 @@ func SetNx(k, v string, timeoutSecond int) error {
return e return e
} }
func Del(ks ...string) (int64,error){ func Del(ks ...string) (int64, error) {
return getDb().Del(ks...).Result() return getDb().Del(ks...).Result()
} }
...@@ -133,8 +133,8 @@ end` ...@@ -133,8 +133,8 @@ end`
// Lock 抢锁。抢到锁返回true,否则false。只抢一次 // Lock 抢锁。抢到锁返回true,否则false。只抢一次
// 所有参数必传,超时时间单位秒 // 所有参数必传,超时时间单位秒
func Lock(k, uniqueValue string, timeoutSecond int) bool { func Lock(k, uniqueValue string, timeoutSecond int) bool {
r,_ := getDb().Eval(lockScript, []string{k}, timeoutSecond, uniqueValue).String() r, _ := getDb().Eval(lockScript, []string{k}, timeoutSecond, uniqueValue).String()
if r=="1" { if r == "1" {
return true return true
} }
return false return false
...@@ -143,19 +143,38 @@ func Lock(k, uniqueValue string, timeoutSecond int) bool { ...@@ -143,19 +143,38 @@ func Lock(k, uniqueValue string, timeoutSecond int) bool {
// Unlock 解锁 // Unlock 解锁
// 所有参数必传 // 所有参数必传
func Unlock(k, uniqueValue string) bool { func Unlock(k, uniqueValue string) bool {
r,_ := getDb().Eval(unlockScript, []string{k}, uniqueValue).String() r, _ := getDb().Eval(unlockScript, []string{k}, uniqueValue).String()
if r=="1" { if r == "1" {
return true return true
} }
return false return false
} }
// LockWait 抢锁,在规定的时间内直到抢锁成功或者超时失败。抢到锁返回true,否则false
// k 必须
// uniqueValue 必须
// timeoutSecond 锁自动释放时间,必须
// lockTimeoutSecond 抢锁超时时间,默认1分钟
func LockWait(k, uniqueValue string, timeoutSecond int, lockTimeoutSecond int) bool {
if lockTimeoutSecond == 0 {
lockTimeoutSecond = 60
}
loopCount := lockTimeoutSecond * 10
for i := 0; i < loopCount; i++ {
if Lock(k, uniqueValue, timeoutSecond) {
return true
}
time.Sleep(time.Duration(100) * time.Millisecond) //100ms
}
return false
}
// Hset 向一张hash表中放入数据,如果不存在将创建 // Hset 向一张hash表中放入数据,如果不存在将创建
func Hset(k, i, v string, timeoutSecond int) error { func Hset(k, i, v string, timeoutSecond int) error {
e := getDb().HSet(k, i, v).Err() e := getDb().HSet(k, i, v).Err()
if e==nil { if e == nil {
log.Debug(fmt.Sprintf("设置缓存成功:%s %s %s", k, i, v)) log.Debug(fmt.Sprintf("设置缓存成功:%s %s %s", k, i, v))
if timeoutSecond >0 { if timeoutSecond > 0 {
Expire(k, int64(timeoutSecond)) Expire(k, int64(timeoutSecond))
} }
} else { } else {
...@@ -165,28 +184,28 @@ func Hset(k, i, v string, timeoutSecond int) error { ...@@ -165,28 +184,28 @@ func Hset(k, i, v string, timeoutSecond int) error {
} }
// HGet 从hash中获取值 // HGet 从hash中获取值
func HGet(k, i string) (string,error) { func HGet(k, i string) (string, error) {
r,e := getDb().HGet(k,i).Result() r, e := getDb().HGet(k, i).Result()
return r,e return r, e
} }
// HGetAll 从hash中获key所有的取值 // HGetAll 从hash中获key所有的取值
func HGetAll(k string) (map[string]string,error) { func HGetAll(k string) (map[string]string, error) {
return getDb().HGetAll(k).Result() return getDb().HGetAll(k).Result()
} }
// Hdel 从hash中删除 // Hdel 从hash中删除
func Hdel(k string, is ...string) (int64,error) { func Hdel(k string, is ...string) (int64, error) {
r,e := getDb().HDel(k, is...).Result() r, e := getDb().HDel(k, is...).Result()
return r,e return r, e
} }
// HMSet 向一张hash表中放入数据,如果不存在将创建 // HMSet 向一张hash表中放入数据,如果不存在将创建
func HMSet(k string, fields map[string]interface{}, timeoutSecond int) error { func HMSet(k string, fields map[string]interface{}, timeoutSecond int) error {
e := getDb().HMSet(k, fields).Err() e := getDb().HMSet(k, fields).Err()
if e==nil { if e == nil {
log.Debug(fmt.Sprintf("设置缓存成功:%s %s", k, fields)) log.Debug(fmt.Sprintf("设置缓存成功:%s %s", k, fields))
if timeoutSecond >0 { if timeoutSecond > 0 {
Expire(k, int64(timeoutSecond)) Expire(k, int64(timeoutSecond))
} }
} else { } else {
...@@ -195,6 +214,6 @@ func HMSet(k string, fields map[string]interface{}, timeoutSecond int) error { ...@@ -195,6 +214,6 @@ func HMSet(k string, fields map[string]interface{}, timeoutSecond int) error {
return e return e
} }
func HMget(k string, is ...string) ([]interface{},error) { func HMget(k string, is ...string) ([]interface{}, error) {
return getDb().HMGet(k, is...).Result() return getDb().HMGet(k, is...).Result()
} }
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 to comment