Documentation ¶
Index ¶
- func DataChangeNotify(opts ...HookOption) ent.Hook
- func Evict(ctx context.Context) context.Context
- func Skip(ctx context.Context) context.Context
- func WithEntryKey(ctx context.Context, typ string, id any) context.Context
- func WithRefEntryKey(ctx context.Context, typ string, id any) context.Context
- func WithTTL(ctx context.Context, ttl time.Duration) context.Context
- type ChangeSet
- func (a *ChangeSet) Delete(key Key)
- func (a *ChangeSet) DeleteRef(key Key)
- func (a *ChangeSet) Load(key Key) (time.Time, bool)
- func (a *ChangeSet) LoadOrStoreRef(key Key) (t time.Time, loaded bool)
- func (a *ChangeSet) LoadRef(key Key) (time.Time, bool)
- func (a *ChangeSet) Start(ctx context.Context) error
- func (a *ChangeSet) Stop(ctx context.Context) error
- func (a *ChangeSet) Store(keys ...Key)
- type Config
- type Driver
- type Entry
- type HookOption
- type Key
- type Option
- type Stats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DataChangeNotify ¶
func DataChangeNotify(opts ...HookOption) ent.Hook
DataChangeNotify returns a hook that notifies the cache when a mutation is performed.
Driver in method is a placeholder for the cache driver name, which is lazy loaded by NewDriver. Use IDs method to get the ids of the mutation, that also works for XXXOne.
func Evict ¶
Evict returns a new Context that tells the Driver to refresh the cache entry on Query.
client.T.Query().All(entcache.Evict(ctx))
func Skip ¶
Skip returns a new Context that tells the Driver to skip the cache entry on Query.
client.T.Query().All(entcache.Skip(ctx))
func WithEntryKey ¶
WithEntryKey returns a new Context that carries the Key for the cache entry. Note that the key is one shot, otherwise cause error if the ent.Client query involves more than 1 SQL query (e.g. eager loading).
func WithRefEntryKey ¶
WithRefEntryKey returns a new Context that carries a reference Entry Key for the cache entry. RefEntryKey indicates if the key is a reference an entry key. For example, when Get is called, ref is false, because Get use id query and get all fields. When others are called, such as Only, ref is false.
Types ¶
type ChangeSet ¶
ChangeSet is a set of keys that have changed, include update, delete, create.
func NewChangeSet ¶
func (*ChangeSet) LoadOrStoreRef ¶
LoadOrStoreRef returns the time when the key was last updated.
type Config ¶
type Config struct { // Name of the driver, used for ent cache driver mandger. Name string `yaml:"name" json:"name"` // Cache defines the cache implementation for holding the cache entries. // Default is tinyLFU with size 100000 and HashQueryTTL 1 minute. Cache cache.Cache `yaml:"-" json:"-"` // HashQueryTTL defines the period of time that an Entry that is hashed through by not Get // is valid in the cache. HashQueryTTL time.Duration `yaml:"hashQueryTTL" json:"hashQueryTTL"` // KeyQueryTTL defines the period of time that an Entry that is not hashed through by Get. This is keep the cached // data fresh, can be set to long time, such as 1 hour. KeyQueryTTL time.Duration `yaml:"keyQueryTTL" json:"keyQueryTTL"` // GCInterval defines the period of time that the cache will be GC. GCInterval time.Duration `yaml:"gcInterval" json:"gcInterval"` // StoreKey is the driver name of cache driver StoreKey string `yaml:"storeKey" json:"storeKey"` // CachePrefix is the prefix of cache key, avoid key conflict in redis cache CachePrefix string `yaml:"cachePrefix" json:"cachePrefix"` // ChangeSet manages data change ChangeSet *ChangeSet }
Config wraps the basic configuration cache options.
type Driver ¶
type Driver struct { *Config dialect.Driver Hash func(query string, args []any) (Key, error) // contains filtered or unexported fields }
A Driver is a SQL cached client. Users should use the constructor below for creating a new driver.
func (*Driver) Query ¶
Query implements the Querier interface for the driver. It falls back to the underlying wrapped driver in case of caching error.
Note that the driver does not synchronize identical queries that are executed concurrently. Hence, if 2 identical queries are executed at the ~same time, and there is no cache entry for them, the driver will execute both of them and the last successful one will be stored in the cache.
type HookOption ¶
type HookOption func(*hookOptions)
func WithDriverName ¶
func WithDriverName(name string) HookOption
WithDriverName sets which named ent cache driver name to use.
type Key ¶
type Key string
func DefaultHash ¶
DefaultHash provides the default implementation for converting a query and its argument to a cache key.
func NewEntryKey ¶
type Option ¶
type Option func(*Config)
Option allows configuring the cache driver using functional options.
func WithChangeSet ¶
func WithConfiguration ¶
func WithConfiguration(cnf *conf.Configuration) Option
WithConfiguration provides a configuration option for the cache driver.