1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-17 12:12:46 -05:00
forgejo/vendor/xorm.io/xorm/caches/cache.go

100 lines
2.5 KiB
Go
Raw Normal View History

// Copyright 2019 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package caches
2016-11-03 18:16:01 -04:00
import (
2018-01-27 10:20:59 -05:00
"bytes"
"encoding/gob"
2016-11-03 18:16:01 -04:00
"errors"
"fmt"
2018-01-27 10:20:59 -05:00
"strings"
2016-11-03 18:16:01 -04:00
"time"
"xorm.io/xorm/schemas"
2016-11-03 18:16:01 -04:00
)
const (
// CacheExpired is default cache expired time
2016-11-03 18:16:01 -04:00
CacheExpired = 60 * time.Minute
// CacheMaxMemory is not use now
2016-11-03 18:16:01 -04:00
CacheMaxMemory = 256
// CacheGcInterval represents interval time to clear all expired nodes
2016-11-03 18:16:01 -04:00
CacheGcInterval = 10 * time.Minute
// CacheGcMaxRemoved represents max nodes removed when gc
2016-11-03 18:16:01 -04:00
CacheGcMaxRemoved = 20
)
// list all the errors
2016-11-03 18:16:01 -04:00
var (
ErrCacheMiss = errors.New("xorm/cache: key not found")
ErrNotStored = errors.New("xorm/cache: not stored")
// ErrNotExist record does not exist error
ErrNotExist = errors.New("Record does not exist")
2016-11-03 18:16:01 -04:00
)
// CacheStore is a interface to store cache
type CacheStore interface {
// key is primary key or composite primary key
// value is struct's pointer
// key format : <tablename>-p-<pk1>-<pk2>...
Put(key string, value interface{}) error
Get(key string) (interface{}, error)
Del(key string) error
}
// Cacher is an interface to provide cache
// id format : u-<pk1>-<pk2>...
type Cacher interface {
GetIds(tableName, sql string) interface{}
GetBean(tableName string, id string) interface{}
PutIds(tableName, sql string, ids interface{})
PutBean(tableName string, id string, obj interface{})
DelIds(tableName, sql string)
DelBean(tableName string, id string)
ClearIds(tableName string)
ClearBeans(tableName string)
}
func encodeIds(ids []schemas.PK) (string, error) {
2016-11-03 18:16:01 -04:00
buf := new(bytes.Buffer)
enc := gob.NewEncoder(buf)
err := enc.Encode(ids)
return buf.String(), err
}
func decodeIds(s string) ([]schemas.PK, error) {
pks := make([]schemas.PK, 0)
2016-11-03 18:16:01 -04:00
2018-01-27 10:20:59 -05:00
dec := gob.NewDecoder(strings.NewReader(s))
2016-11-03 18:16:01 -04:00
err := dec.Decode(&pks)
return pks, err
}
// GetCacheSql returns cacher PKs via SQL
func GetCacheSql(m Cacher, tableName, sql string, args interface{}) ([]schemas.PK, error) {
2016-11-03 18:16:01 -04:00
bytes := m.GetIds(tableName, GenSqlKey(sql, args))
if bytes == nil {
return nil, errors.New("Not Exist")
}
return decodeIds(bytes.(string))
}
// PutCacheSql puts cacher SQL and PKs
func PutCacheSql(m Cacher, ids []schemas.PK, tableName, sql string, args interface{}) error {
2016-11-03 18:16:01 -04:00
bytes, err := encodeIds(ids)
if err != nil {
return err
}
m.PutIds(tableName, GenSqlKey(sql, args), bytes)
return nil
}
// GenSqlKey generates cache key
2016-11-03 18:16:01 -04:00
func GenSqlKey(sql string, args interface{}) string {
return fmt.Sprintf("%v-%v", sql, args)
}