adapter
packageAPI reference for the adapter
package.
Imports
(11)Store
Store abstracts the primary storage used as fallback and warmup source.
T represents the type of values stored in the adapter.
type Store interface
Methods
Batch
Batch allows grouping multiple operations before committing them to the
underlying storage.
type Batch interface
Methods
Batcher
Batcher is implemented by stores that support batch operations.
type Batcher interface
Methods
InMemoryStore
InMemoryStore is a simple Store implementation backed by a map.
type InMemoryStore struct
Fields
| Name | Type | Description |
|---|---|---|
| mu | sync.RWMutex | |
| items | map[string]T |
NewInMemoryStore
NewInMemoryStore returns a new InMemoryStore.
Returns
func NewInMemoryStore[T any]() *InMemoryStore[T]
{
return &InMemoryStore[T]{items: make(map[string]T)}
}
inMemoryBatch
type inMemoryBatch struct
Fields
| Name | Type | Description |
|---|---|---|
| s | *InMemoryStore[T] | |
| sets | map[string]T | |
| deletes | []string |
GormEntityStore
GormEntityStore implements Store by mapping keys directly to a GORM model’s primary key.
Unlike GormStore, which uses a KV table to store serialized blobs, GormEntityStore
operates directly on the entity table defined by T.
T should be a struct type that GORM recognizes as a model.
type GormEntityStore struct
Fields
| Name | Type | Description |
|---|---|---|
| db | *gorm.DB | |
| keyColumn | string | |
| keyParser | func(string) (any, error) |
EntityOption
EntityOption configures a GormEntityStore.
type EntityOption func(*entityStoreOptions)
entityStoreOptions
type entityStoreOptions struct
Fields
| Name | Type | Description |
|---|---|---|
| keyColumn | string | |
| keyParser | func(string) (any, error) |
WithEntityKeyColumn
WithEntityKeyColumn sets the column name to use for lookups (default “id”).
Parameters
Returns
func WithEntityKeyColumn(col string) EntityOption
{
return func(o *entityStoreOptions) {
o.keyColumn = col
}
}
WithEntityKeyParser
WithEntityKeyParser sets a function to convert the string key to the DB key type.
Default is to pass the string as-is.
Useful for integer primary keys: use adapter.ParseIntKey
Parameters
Returns
func WithEntityKeyParser(parser func(string) (any, error)) EntityOption
{
return func(o *entityStoreOptions) {
o.keyParser = parser
}
}
ParseIntKey
ParseIntKey is a helper for WithEntityKeyParser to convert keys to integers.
Parameters
Returns
func ParseIntKey(s string) (any, error)
{
return strconv.Atoi(s)
}
NewGormEntityStore
NewGormEntityStore creates a new GormEntityStore.
T is the model struct.
Parameters
Returns
func NewGormEntityStore[T any](db *gorm.DB, opts ...EntityOption) *GormEntityStore[T]
{
o := entityStoreOptions{
keyColumn: "id",
keyParser: func(s string) (any, error) { return s, nil },
}
for _, opt := range opts {
opt(&o)
}
return &GormEntityStore[T]{
db: db,
keyColumn: o.keyColumn,
keyParser: o.keyParser,
}
}
gormEntityBatch
type gormEntityBatch struct
Fields
| Name | Type | Description |
|---|---|---|
| s | *GormEntityStore[T] | |
| sets | []T | |
| deletes | []any |
gormKV
gormKV is the internal model used to store key-value pairs in the database.
type gormKV struct
Fields
| Name | Type | Description |
|---|---|---|
| Key | string | gorm:"primaryKey;column:key_id" |
| Value | []byte | gorm:"column:value" |
GormStore
GormStore implements Store using a GORM backend.
type GormStore struct
Fields
| Name | Type | Description |
|---|---|---|
| db | *gorm.DB | |
| tableName | string | |
| timeout | time.Duration | |
| codec | cache.Codec |
GormOption
GormOption configures a GormStore.
type GormOption func(*gormStoreOptions)
gormStoreOptions
type gormStoreOptions struct
Fields
| Name | Type | Description |
|---|---|---|
| tableName | string | |
| timeout | time.Duration | |
| codec | cache.Codec |
WithGormTableName
WithGormTableName sets the table name for the GormStore.
Parameters
Returns
func WithGormTableName(name string) GormOption
{
return func(o *gormStoreOptions) {
o.tableName = name
}
}
Uses
WithGormTimeout
WithGormTimeout sets the operation timeout for GORM calls.
Parameters
Returns
func WithGormTimeout(d time.Duration) GormOption
{
return func(o *gormStoreOptions) {
o.timeout = d
}
}
Uses
WithGormCodec
WithGormCodec sets the codec for serialization.
Parameters
Returns
func WithGormCodec(c cache.Codec) GormOption
{
return func(o *gormStoreOptions) {
o.codec = c
}
}
Uses
NewGormStore
NewGormStore returns a new GormStore using the provided GORM DB connection.
Parameters
Returns
func NewGormStore[T any](db *gorm.DB, opts ...GormOption) *GormStore[T]
{
o := gormStoreOptions{
tableName: defaultGormTableName,
timeout: defaultGormOpTimeout,
codec: cache.GobCodec{},
}
for _, opt := range opts {
opt(&o)
}
// Ensure the table exists
if !db.Migrator().HasTable(o.tableName) {
_ = db.Table(o.tableName).AutoMigrate(&gormKV{})
}
return &GormStore[T]{
db: db,
tableName: o.tableName,
timeout: o.timeout,
codec: o.codec,
}
}
gormBatch
type gormBatch struct
Fields
| Name | Type | Description |
|---|---|---|
| s | *GormStore[T] | |
| sets | map[string]T | |
| deletes | []string |
RedisStore
RedisStore implements Store using a Redis backend.
type RedisStore struct
Fields
| Name | Type | Description |
|---|---|---|
| client | *redis.Client | |
| timeout | time.Duration | |
| codec | cache.Codec |
RedisOption
RedisOption configures a RedisStore.
type RedisOption func(*redisStoreOptions)
redisStoreOptions
type redisStoreOptions struct
Fields
| Name | Type | Description |
|---|---|---|
| timeout | time.Duration | |
| codec | cache.Codec |
WithTimeout
WithTimeout sets the operation timeout for Redis calls.
Parameters
Returns
func WithTimeout(d time.Duration) RedisOption
{
return func(o *redisStoreOptions) {
o.timeout = d
}
}
WithCodec
WithCodec sets the codec for serialization.
Parameters
Returns
func WithCodec(c cache.Codec) RedisOption
{
return func(o *redisStoreOptions) {
o.codec = c
}
}
NewRedisStore
NewRedisStore returns a new RedisStore using the provided Redis client.
Parameters
Returns
func NewRedisStore[T any](client *redis.Client, opts ...RedisOption) *RedisStore[T]
{
o := redisStoreOptions{
timeout: defaultRedisOpTimeout,
codec: cache.GobCodec{},
}
for _, opt := range opts {
opt(&o)
}
return &RedisStore[T]{client: client, timeout: o.timeout, codec: o.codec}
}
redisBatch
type redisBatch struct
Fields
| Name | Type | Description |
|---|---|---|
| s | *RedisStore[T] | |
| sets | map[string]T | |
| deletes | []string |