You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
2.5 KiB
100 lines
2.5 KiB
6 years ago
|
// 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.
|
||
|
|
||
5 years ago
|
package caches
|
||
8 years ago
|
|
||
|
import (
|
||
7 years ago
|
"bytes"
|
||
|
"encoding/gob"
|
||
8 years ago
|
"errors"
|
||
|
"fmt"
|
||
7 years ago
|
"strings"
|
||
8 years ago
|
"time"
|
||
5 years ago
|
|
||
|
"xorm.io/xorm/schemas"
|
||
8 years ago
|
)
|
||
|
|
||
|
const (
|
||
5 years ago
|
// CacheExpired is default cache expired time
|
||
8 years ago
|
CacheExpired = 60 * time.Minute
|
||
5 years ago
|
// CacheMaxMemory is not use now
|
||
8 years ago
|
CacheMaxMemory = 256
|
||
5 years ago
|
// CacheGcInterval represents interval time to clear all expired nodes
|
||
8 years ago
|
CacheGcInterval = 10 * time.Minute
|
||
5 years ago
|
// CacheGcMaxRemoved represents max nodes removed when gc
|
||
8 years ago
|
CacheGcMaxRemoved = 20
|
||
|
)
|
||
|
|
||
5 years ago
|
// list all the errors
|
||
8 years ago
|
var (
|
||
5 years ago
|
ErrCacheMiss = errors.New("xorm/cache: key not found")
|
||
|
ErrNotStored = errors.New("xorm/cache: not stored")
|
||
5 years ago
|
// ErrNotExist record does not exist error
|
||
|
ErrNotExist = errors.New("Record does not exist")
|
||
8 years ago
|
)
|
||
|
|
||
|
// 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)
|
||
|
}
|
||
|
|
||
5 years ago
|
func encodeIds(ids []schemas.PK) (string, error) {
|
||
8 years ago
|
buf := new(bytes.Buffer)
|
||
|
enc := gob.NewEncoder(buf)
|
||
|
err := enc.Encode(ids)
|
||
|
|
||
|
return buf.String(), err
|
||
|
}
|
||
|
|
||
5 years ago
|
func decodeIds(s string) ([]schemas.PK, error) {
|
||
|
pks := make([]schemas.PK, 0)
|
||
8 years ago
|
|
||
7 years ago
|
dec := gob.NewDecoder(strings.NewReader(s))
|
||
8 years ago
|
err := dec.Decode(&pks)
|
||
|
|
||
|
return pks, err
|
||
|
}
|
||
|
|
||
5 years ago
|
// GetCacheSql returns cacher PKs via SQL
|
||
5 years ago
|
func GetCacheSql(m Cacher, tableName, sql string, args interface{}) ([]schemas.PK, error) {
|
||
8 years ago
|
bytes := m.GetIds(tableName, GenSqlKey(sql, args))
|
||
|
if bytes == nil {
|
||
|
return nil, errors.New("Not Exist")
|
||
|
}
|
||
|
return decodeIds(bytes.(string))
|
||
|
}
|
||
|
|
||
5 years ago
|
// PutCacheSql puts cacher SQL and PKs
|
||
5 years ago
|
func PutCacheSql(m Cacher, ids []schemas.PK, tableName, sql string, args interface{}) error {
|
||
8 years ago
|
bytes, err := encodeIds(ids)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
m.PutIds(tableName, GenSqlKey(sql, args), bytes)
|
||
|
return nil
|
||
|
}
|
||
|
|
||
5 years ago
|
// GenSqlKey generates cache key
|
||
8 years ago
|
func GenSqlKey(sql string, args interface{}) string {
|
||
|
return fmt.Sprintf("%v-%v", sql, args)
|
||
|
}
|