Documentation ¶
Index ¶
- Variables
- func Evict(ctx context.Context) context.Context
- func NewContext(ctx context.Context, levels ...AddGetDeleter) context.Context
- func Skip(ctx context.Context) context.Context
- func WithKey(ctx context.Context, key Key) context.Context
- func WithTTL(ctx context.Context, ttl time.Duration) context.Context
- type AddGetDeleter
- type Driver
- func (d *Driver) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (d *Driver) Query(ctx context.Context, query string, args, v any) error
- func (d *Driver) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (d *Driver) Stats() Stats
- type Entry
- type Key
- type LRU
- type Option
- type Options
- type Redis
- type Stats
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("entcache: entry was not found")
ErrNotFound is returned by Get when and Entry does not exist in the cache.
Functions ¶
func Evict ¶
Evict returns a new Context that tells the Driver to skip and invalidate the cache entry on Query.
client.T.Query().All(entcache.Evict(ctx))
func NewContext ¶
func NewContext(ctx context.Context, levels ...AddGetDeleter) context.Context
NewContext returns a new Context that carries a cache.
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))
Types ¶
type AddGetDeleter ¶
type AddGetDeleter interface { Del(context.Context, Key) error Add(context.Context, Key, *Entry, time.Duration) error Get(context.Context, Key) (*Entry, error) }
AddGetDeleter defines the interface for getting, adding and deleting entries from the cache.
func FromContext ¶
func FromContext(ctx context.Context) (AddGetDeleter, bool)
FromContext returns the cache value stored in ctx, if any.
type Driver ¶
A Driver is an SQL cached client. Users should use the constructor below for creating new driver.
func NewDriver ¶
NewDriver returns a new Driver an existing driver and optional configuration functions. For example:
entcache.NewDriver( drv, entcache.TTL(time.Minute), entcache.Levels( NewLRU(256), NewRedis(redis.NewClient(&redis.Options{ Addr: ":6379", })), ) )
func (*Driver) ExecContext ¶
ExecContext calls ExecContext of the underlying driver, or fails if it is not supported.
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.
func (*Driver) QueryContext ¶
QueryContext calls QueryContext of the underlying driver, or fails if it is not supported. Note, this method is not part of the caching layer since Ent does not use it by default.
type Entry ¶
Entry defines an entry to store in a cache.
func (Entry) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaler interface.
func (*Entry) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
type Key ¶
type Key any
A Key defines a comparable Go value. See http://golang.org/ref/spec#Comparison_operators
type LRU ¶
LRU provides an LRU cache that implements the AddGetter interface.
type Option ¶
type Option func(*Options)
Option allows configuring the cache driver using functional options.
func ContextLevel ¶
func ContextLevel() Option
ContextLevel configures the driver to work with context/request level cache. Users that use this option, should wraps the *http.Request context with the cache value as follows:
ctx = entcache.NewContext(ctx) ctx = entcache.NewContext(ctx, entcache.NewLRU(128))
func Hash ¶
Hash configures an optional Hash function for converting a query and its arguments to a cache key.
func Levels ¶
func Levels(levels ...AddGetDeleter) Option
Levels configures the Driver to work with the given cache levels. For example, in process LRU cache and a remote Redis cache.
type Options ¶
type Options struct { // TTL defines the period of time that an Entry // is valid in the cache. TTL time.Duration // Cache defines the GetAddDeleter (cache implementation) // for holding the cache entries. If no cache implementation // was provided, an LRU cache with no limit is used. Cache AddGetDeleter // Hash defines an optional Hash function for converting // a query and its arguments to a cache key. If no Hash // function was provided, the DefaultHash is used. Hash func(query string, args []any) (Key, error) // Logf function. If provided, the Driver will call it with // errors that can not be handled. Log func(...any) }
Options wraps the basic configuration cache options.
type Redis ¶
type Redis struct {
// contains filtered or unexported fields
}
Redis provides a remote cache backed by Redis and implements the SetGetter interface.
func NewRedis ¶
func NewRedis(c redis.Cmdable) *Redis
NewRedis returns a new Redis cache level from the given Redis connection.
entcache.NewRedis(redis.NewClient(&redis.Options{ Addr: ":6379" })) entcache.NewRedis(redis.NewClusterClient(&redis.ClusterOptions{ Addrs: []string{":7000", ":7001", ":7002"}, }))