Skip to content

Instantly share code, notes, and snippets.

@aloha1003
Last active August 3, 2020 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aloha1003/7a66b6f2b1540804cfd64ebf1eae49a5 to your computer and use it in GitHub Desktop.
Save aloha1003/7a66b6f2b1540804cfd64ebf1eae49a5 to your computer and use it in GitHub Desktop.
golang grom repository
func (r *RedisDao) DelByPattern(pattern string) error {
keys, err := redis.Strings(r.conn.Do("KEYS", pattern))
if err != nil {
// handle error
}
for _, key := range keys {
_, err = r.conn.Do("DEL", key)
// r.Del(key)
}
return nil
}
//使用方法
log := model.LogYilou{}
queryResult := []model.LogYilou{}
search["deleted"] = model.NOT_DELETED
var options = map[string]interface{}{
"search": search,
"page": page,
"perpage": perpage,
"sort": repo.GetSort(sort, log.SortMap()),
}
repo.List(log.TableName(), &queryResult, options)
var inputValues = map[string]interface{}{
"round_id": 55,
}
repo.Save(log.TableName(), &log, inputValues)
func List(tableName string, rows interface{}, options map[string]interface{}) (int64, error) {
search, _ := options["search"].(map[string]interface{})
page, _ := options["page"].(int64)
perpage, _ := options["perpage"].(int64)
if page == 0 {
page = int64(1)
}
if perpage == 0 {
perpage = int64(30)
}
sort, _ := options["sort"].(string)
cond, vals, err := WhereBuild(search)
if err != nil {
return 0, err
}
var count int64
//設定快取
enableCache := config.GetBool("repository.enableCache")
redis := redisdao.Redis()
cacheKey := CacheKey(tableName, page, search, perpage, sort)
countCacheKey := cacheKey + "|count"
if enableCache && false {
redisResultJsonString, _ := redis.GetBytes(cacheKey)
countRedisString, _ := redis.Get(countCacheKey)
if len(redisResultJsonString) == 0 || countRedisString == "" {
if err := DB().Table(tableName).Where(cond, vals...).Order(sort).Offset(page * perpage).Scan(rows).Count(&count).Error; err != nil {
return 0, err
}
jsonString, _ := json.Marshal(rows)
redis.SetEx(cacheKey, config.GetInt("repository.ttl"), jsonString)
redis.SetEx(countCacheKey, config.GetInt("repository.ttl"), strconv.FormatInt(int64(count), 10))
} else {
json.Unmarshal(redisResultJsonString, &rows)
count, _ = strconv.ParseInt(countRedisString, 10, 64)
}
} else {
if err := DB().Table(tableName).Where(cond, vals...).Order(sort).Offset(page * perpage).Scan(rows).Count(&count).Error; err != nil {
return count, err
}
}
return count, nil
}
func Get(tableName string, rows interface{}, id string) error {
//設定快取
enableCache := config.GetBool("repository.enableCache")
redis := redisdao.Redis()
cacheKey := CacheKey(tableName)
countCacheKey := cacheKey + "|1"
if enableCache {
redisResultJsonString, _ := redis.GetBytes(cacheKey)
countRedisString, _ := redis.Get(countCacheKey)
if len(redisResultJsonString) == 0 || countRedisString == "" {
if err := DB().Table(tableName).Where("id = ?", id).Scan(rows).Error; err != nil {
return err
}
jsonString, _ := json.Marshal(rows)
redis.SetEx(cacheKey, config.GetInt("repository.ttl"), jsonString)
} else {
json.Unmarshal(redisResultJsonString, rows)
}
} else {
if err := DB().Table(tableName).Where("id = ?", id).Scan(rows).Error; err != nil {
return err
}
}
return nil
}
func Save(tableName string, current interface{}, values map[string]interface{}) {
tx := DB()
if id, ok := values["id"]; ok {
fmt.Println("B")
if id != "" {
tx.Model(current).Updates(values).Where("id=?", id)
} else {
tx.FirstOrCreate(current)
}
} else {
tx.Table(tableName).Save(current)
tx.Model(current).Updates(values)
}
//設定快取
enableCache := config.GetBool("repository.enableCache")
if enableCache {
//刪除快取
redis := redisdao.Redis()
cacheKeyPerfix := CacheKey(tableName + "*")
redis.DelByPattern(cacheKeyPerfix)
}
}
func Delete(tableName string, current interface{}) error {
err := DB().Delete(&current).Error
return err
}
func CacheKey(prefix string, vals ...interface{}) string {
str := ""
if len(vals) > 0 {
for _, v := range vals {
str = str + "|" + fmt.Sprint(v)
}
str = prefix + str
}
return str
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment