forked from free-lancers/electricity_bill_calc_service
refactor(db):切换数据库驱动为pq,数据控制库为bun,更改Redis连接的名称。
This commit is contained in:
28
cache/abstract.go
vendored
28
cache/abstract.go
vendored
@@ -25,24 +25,24 @@ func Cache[T interface{}](key string, value *T, expires time.Duration) error {
|
||||
var err error
|
||||
if expires > 0 {
|
||||
realExpires := expires + time.Duration(rand.Int63n(60))*time.Second
|
||||
setCmd := global.RedisConn.B().Set().
|
||||
setCmd := global.Rd.B().Set().
|
||||
Key(key).Value(rueidis.JSON(value)).
|
||||
ExSeconds(int64(realExpires.Seconds())).
|
||||
Build()
|
||||
err = global.RedisConn.Do(global.Ctx, setCmd).Error()
|
||||
err = global.Rd.Do(global.Ctx, setCmd).Error()
|
||||
} else {
|
||||
setCmd := global.RedisConn.B().Set().
|
||||
setCmd := global.Rd.B().Set().
|
||||
Key(key).Value(rueidis.JSON(value)).
|
||||
Build()
|
||||
err = global.RedisConn.Do(global.Ctx, setCmd).Error()
|
||||
err = global.Rd.Do(global.Ctx, setCmd).Error()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// 从Redis缓存中获取一个数据
|
||||
func Retreive[T interface{}](key string) (*T, error) {
|
||||
getCmd := global.RedisConn.B().Get().Key(key).Build()
|
||||
result := global.RedisConn.Do(global.Ctx, getCmd)
|
||||
getCmd := global.Rd.B().Get().Key(key).Build()
|
||||
result := global.Rd.Do(global.Ctx, getCmd)
|
||||
if result.Error() != nil {
|
||||
if rueidis.IsRedisNil(result.Error()) {
|
||||
return nil, nil
|
||||
@@ -60,8 +60,8 @@ func Retreive[T interface{}](key string) (*T, error) {
|
||||
|
||||
// 检查Redis缓存中是否存在指定键的记录
|
||||
func Exists(key string) (bool, error) {
|
||||
existsCmd := global.RedisConn.B().Exists().Key(key).Build()
|
||||
result := global.RedisConn.Do(global.Ctx, existsCmd)
|
||||
existsCmd := global.Rd.B().Exists().Key(key).Build()
|
||||
result := global.Rd.Do(global.Ctx, existsCmd)
|
||||
if result.Error() != nil {
|
||||
return false, result.Error()
|
||||
}
|
||||
@@ -72,8 +72,8 @@ func Exists(key string) (bool, error) {
|
||||
// 从Redis缓存中删除指定键
|
||||
// ! 如果指定键已不存在,那么本函数一样会返回false
|
||||
func Delete(key string) (bool, error) {
|
||||
deleteCmd := global.RedisConn.B().Del().Key(key).Build()
|
||||
result := global.RedisConn.Do(global.Ctx, deleteCmd)
|
||||
deleteCmd := global.Rd.B().Del().Key(key).Build()
|
||||
result := global.Rd.Do(global.Ctx, deleteCmd)
|
||||
if result.Error() != nil {
|
||||
return false, result.Error()
|
||||
}
|
||||
@@ -111,8 +111,8 @@ func DeleteAll(pattern string) error {
|
||||
sKeys []string
|
||||
)
|
||||
for {
|
||||
scanCmd := global.RedisConn.B().Scan().Cursor(cursor).Match(pattern).Count(20).Build()
|
||||
results := global.RedisConn.Do(global.Ctx, scanCmd)
|
||||
scanCmd := global.Rd.B().Scan().Cursor(cursor).Match(pattern).Count(20).Build()
|
||||
results := global.Rd.Do(global.Ctx, scanCmd)
|
||||
cursor, sKeys, err = dissembleScan(results)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -123,8 +123,8 @@ func DeleteAll(pattern string) error {
|
||||
}
|
||||
}
|
||||
|
||||
delCmd := global.RedisConn.B().Del().Key(keys...).Build()
|
||||
err = global.RedisConn.Do(global.Ctx, delCmd).Error()
|
||||
delCmd := global.Rd.B().Del().Key(keys...).Build()
|
||||
err = global.Rd.Do(global.Ctx, delCmd).Error()
|
||||
return err
|
||||
}
|
||||
|
||||
|
28
cache/relation.go
vendored
28
cache/relation.go
vendored
@@ -32,16 +32,16 @@ func assembleRelationIdentity(storeType, key string, field ...string) string {
|
||||
func CacheRelation(relationName, storeType, key string, field ...string) error {
|
||||
relationKey := assembleRelationKey(relationName)
|
||||
relationIdentity := assembleRelationIdentity(storeType, key, field...)
|
||||
cmd := global.RedisConn.B().Sadd().Key(relationKey).Member(relationIdentity).Build()
|
||||
result := global.RedisConn.Do(global.Ctx, cmd)
|
||||
cmd := global.Rd.B().Sadd().Key(relationKey).Member(relationIdentity).Build()
|
||||
result := global.Rd.Do(global.Ctx, cmd)
|
||||
return result.Error()
|
||||
}
|
||||
|
||||
// 从缓存中清理指定的关联键
|
||||
func AbolishRelation(relationName string) error {
|
||||
relationKey := assembleRelationKey(relationName)
|
||||
cmd := global.RedisConn.B().Smembers().Key(relationKey).Build()
|
||||
relationItems, err := global.RedisConn.Do(global.Ctx, cmd).AsStrSlice()
|
||||
cmd := global.Rd.B().Smembers().Key(relationKey).Build()
|
||||
relationItems, err := global.Rd.Do(global.Ctx, cmd).AsStrSlice()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -50,17 +50,17 @@ func AbolishRelation(relationName string) error {
|
||||
separated := strings.Split(item, ";")
|
||||
switch separated[0] {
|
||||
case STORE_TYPE_KEY:
|
||||
cmd := global.RedisConn.B().Del().Key(separated[1]).Build()
|
||||
cmd := global.Rd.B().Del().Key(separated[1]).Build()
|
||||
cmds = append(cmds, cmd)
|
||||
case STORE_TYPE_HASH:
|
||||
cmd := global.RedisConn.B().Hdel().Key(separated[1]).Field(separated[2:]...).Build()
|
||||
cmd := global.Rd.B().Hdel().Key(separated[1]).Field(separated[2:]...).Build()
|
||||
cmds = append(cmds, cmd)
|
||||
case STORE_TYPE_SET:
|
||||
cmd := global.RedisConn.B().Srem().Key(separated[1]).Member(separated[2:]...).Build()
|
||||
cmd := global.Rd.B().Srem().Key(separated[1]).Member(separated[2:]...).Build()
|
||||
cmds = append(cmds, cmd)
|
||||
}
|
||||
}
|
||||
errs := global.RedisConn.DoMulti(global.Ctx, cmds...)
|
||||
errs := global.Rd.DoMulti(global.Ctx, cmds...)
|
||||
firstErr, has := lo.Find(errs, func(elem rueidis.RedisResult) bool {
|
||||
return elem.Error() != nil
|
||||
})
|
||||
@@ -79,8 +79,8 @@ func ClearOrphanRelationItems() error {
|
||||
sKeys []string
|
||||
)
|
||||
for {
|
||||
scanCmd := global.RedisConn.B().Scan().Cursor(cursor).Match(fmt.Sprintf("%s:*", TAG_RELATION)).Count(20).Build()
|
||||
results := global.RedisConn.Do(global.Ctx, scanCmd)
|
||||
scanCmd := global.Rd.B().Scan().Cursor(cursor).Match(fmt.Sprintf("%s:*", TAG_RELATION)).Count(20).Build()
|
||||
results := global.Rd.Do(global.Ctx, scanCmd)
|
||||
cursor, sKeys, err = dissembleScan(results)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -92,8 +92,8 @@ func ClearOrphanRelationItems() error {
|
||||
}
|
||||
var cmds = make(rueidis.Commands, 0)
|
||||
for _, key := range keys {
|
||||
relationItemsCmd := global.RedisConn.B().Smembers().Key(key).Build()
|
||||
results := global.RedisConn.Do(global.Ctx, relationItemsCmd)
|
||||
relationItemsCmd := global.Rd.B().Smembers().Key(key).Build()
|
||||
results := global.Rd.Do(global.Ctx, relationItemsCmd)
|
||||
relationItems, err := results.AsStrSlice()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -105,12 +105,12 @@ func ClearOrphanRelationItems() error {
|
||||
return err
|
||||
}
|
||||
if !exist {
|
||||
cmd := global.RedisConn.B().Srem().Key(key).Member(item).Build()
|
||||
cmd := global.Rd.B().Srem().Key(key).Member(item).Build()
|
||||
cmds = append(cmds, cmd)
|
||||
}
|
||||
}
|
||||
}
|
||||
errs := global.RedisConn.DoMulti(global.Ctx, cmds...)
|
||||
errs := global.Rd.DoMulti(global.Ctx, cmds...)
|
||||
firstErr, has := lo.Find(errs, func(elem rueidis.RedisResult) bool {
|
||||
return elem.Error() != nil
|
||||
})
|
||||
|
Reference in New Issue
Block a user