Documentation ¶
Index ¶
- Variables
- func IsRecordNotFound(err error) bool
- type AggregateCursor
- type Arbitrary
- type FtAggregateIndex
- type FtCreateSchema
- type FtSearchIndex
- type HashRepository
- func (r *HashRepository[T]) Aggregate(ctx context.Context, cmdFn func(agg FtAggregateIndex) rueidis.Completed) (cursor *AggregateCursor, err error)
- func (r *HashRepository[T]) CreateIndex(ctx context.Context, cmdFn func(schema FtCreateSchema) rueidis.Completed) error
- func (r *HashRepository[T]) DropIndex(ctx context.Context) error
- func (r *HashRepository[T]) Fetch(ctx context.Context, id string) (v *T, err error)
- func (r *HashRepository[T]) FetchCache(ctx context.Context, id string, ttl time.Duration) (v *T, err error)
- func (r *HashRepository[T]) IndexName() string
- func (r *HashRepository[T]) NewEntity() (entity *T)
- func (r *HashRepository[T]) Remove(ctx context.Context, id string) error
- func (r *HashRepository[T]) Save(ctx context.Context, entity *T) (err error)
- func (r *HashRepository[T]) SaveMulti(ctx context.Context, entities ...*T) []error
- func (r *HashRepository[T]) Search(ctx context.Context, cmdFn func(search FtSearchIndex) rueidis.Completed) (n int64, s []*T, err error)
- type JSONRepository
- func (r *JSONRepository[T]) Aggregate(ctx context.Context, cmdFn func(agg FtAggregateIndex) rueidis.Completed) (cursor *AggregateCursor, err error)
- func (r *JSONRepository[T]) CreateIndex(ctx context.Context, cmdFn func(schema FtCreateSchema) rueidis.Completed) error
- func (r *JSONRepository[T]) DropIndex(ctx context.Context) error
- func (r *JSONRepository[T]) Fetch(ctx context.Context, id string) (v *T, err error)
- func (r *JSONRepository[T]) FetchCache(ctx context.Context, id string, ttl time.Duration) (v *T, err error)
- func (r *JSONRepository[T]) IndexName() string
- func (r *JSONRepository[T]) NewEntity() *T
- func (r *JSONRepository[T]) Remove(ctx context.Context, id string) error
- func (r *JSONRepository[T]) Save(ctx context.Context, entity *T) (err error)
- func (r *JSONRepository[T]) SaveMulti(ctx context.Context, entities ...*T) []error
- func (r *JSONRepository[T]) Search(ctx context.Context, cmdFn func(search FtSearchIndex) rueidis.Completed) (n int64, s []*T, err error)
- type Repository
- type RepositoryOption
Constants ¶
This section is empty.
Variables ¶
var ( // ErrVersionMismatch indicates that the optimistic update failed. That is someone else had already changed the entity. ErrVersionMismatch = errors.New("object version mismatched, please retry") // ErrEmptyHashRecord indicates the requested hash entity is not found. ErrEmptyHashRecord = errors.New("hash object not found") )
var EndOfCursor = errors.New("end of cursor")
Functions ¶
func IsRecordNotFound ¶
IsRecordNotFound checks if the error is indicating the requested entity is not found.
Types ¶
type AggregateCursor ¶
type AggregateCursor struct {
// contains filtered or unexported fields
}
AggregateCursor unifies the response of FT.AGGREGATE with or without WITHCURSOR
func (*AggregateCursor) Del ¶
func (c *AggregateCursor) Del(ctx context.Context) (err error)
Del uses FT.CURSOR DEL to destroy the cursor
func (*AggregateCursor) Read ¶
Read return the partial result from the initial FT.AGGREGATE This may invoke FT.CURSOR READ to retrieve further result
func (*AggregateCursor) Total ¶
func (c *AggregateCursor) Total() int64
Total return the total numbers of record of the initial FT.AGGREGATE result
type Arbitrary ¶
Arbitrary is alias to cmds.Arbitrary. This allows user build arbitrary command in Repository.CreateIndex
type FtAggregateIndex ¶
type FtAggregateIndex = cmds.FtAggregateIndex
FtAggregateIndex is the FT.AGGREGATE command builder
type FtCreateSchema ¶
type FtCreateSchema = cmds.FtCreateSchema
FtCreateSchema is the FT.CREATE command builder
type FtSearchIndex ¶
type FtSearchIndex = cmds.FtSearchIndex
FtSearchIndex is the FT.SEARCH command builder
type HashRepository ¶
type HashRepository[T any] struct { // contains filtered or unexported fields }
HashRepository is an OM repository backed by redis hash.
func (*HashRepository[T]) Aggregate ¶
func (r *HashRepository[T]) Aggregate(ctx context.Context, cmdFn func(agg FtAggregateIndex) rueidis.Completed) (cursor *AggregateCursor, err error)
Aggregate performs the FT.AGGREGATE and returns a *AggregateCursor for accessing the results
func (*HashRepository[T]) CreateIndex ¶
func (r *HashRepository[T]) CreateIndex(ctx context.Context, cmdFn func(schema FtCreateSchema) rueidis.Completed) error
CreateIndex uses FT.CREATE from the RediSearch module to create inverted index under the name `hashidx:{prefix}` You can use the cmdFn parameter to mutate the index construction command.
func (*HashRepository[T]) DropIndex ¶
func (r *HashRepository[T]) DropIndex(ctx context.Context) error
DropIndex uses FT.DROPINDEX from the RediSearch module to drop index whose name is `hashidx:{prefix}`
func (*HashRepository[T]) Fetch ¶
func (r *HashRepository[T]) Fetch(ctx context.Context, id string) (v *T, err error)
Fetch an entity whose name is `{prefix}:{id}`
func (*HashRepository[T]) FetchCache ¶
func (r *HashRepository[T]) FetchCache(ctx context.Context, id string, ttl time.Duration) (v *T, err error)
FetchCache is like Fetch, but it uses client side caching mechanism.
func (*HashRepository[T]) IndexName ¶
func (r *HashRepository[T]) IndexName() string
IndexName returns the index name used in the FT.CREATE
func (*HashRepository[T]) NewEntity ¶
func (r *HashRepository[T]) NewEntity() (entity *T)
NewEntity returns an empty entity and will have the `redis:",key"` field be set with ULID automatically.
func (*HashRepository[T]) Remove ¶
func (r *HashRepository[T]) Remove(ctx context.Context, id string) error
Remove the entity under the redis key of `{prefix}:{id}`.
func (*HashRepository[T]) Save ¶
func (r *HashRepository[T]) Save(ctx context.Context, entity *T) (err error)
Save the entity under the redis key of `{prefix}:{id}`. It also uses the `redis:",ver"` field and lua script to perform optimistic locking and prevent lost update.
func (*HashRepository[T]) SaveMulti ¶
func (r *HashRepository[T]) SaveMulti(ctx context.Context, entities ...*T) []error
SaveMulti batches multiple HashRepository.Save at once
func (*HashRepository[T]) Search ¶
func (r *HashRepository[T]) Search(ctx context.Context, cmdFn func(search FtSearchIndex) rueidis.Completed) (n int64, s []*T, err error)
Search uses FT.SEARCH from the RediSearch module to search the index whose name is `hashidx:{prefix}` It returns three values: 1. total count of match results inside the redis, and note that it might be larger than returned search result. 2. search result, and note that its length might smaller than the first return value. 3. error if any You can use the cmdFn parameter to mutate the search command.
type JSONRepository ¶
type JSONRepository[T any] struct { // contains filtered or unexported fields }
JSONRepository is an OM repository backed by RedisJSON.
func (*JSONRepository[T]) Aggregate ¶
func (r *JSONRepository[T]) Aggregate(ctx context.Context, cmdFn func(agg FtAggregateIndex) rueidis.Completed) (cursor *AggregateCursor, err error)
Aggregate performs the FT.AGGREGATE and returns a *AggregateCursor for accessing the results
func (*JSONRepository[T]) CreateIndex ¶
func (r *JSONRepository[T]) CreateIndex(ctx context.Context, cmdFn func(schema FtCreateSchema) rueidis.Completed) error
CreateIndex uses FT.CREATE from the RediSearch module to create inverted index under the name `jsonidx:{prefix}` You can use the cmdFn parameter to mutate the index construction command, and note that the field name should be specified with JSON path syntax, otherwise the index may not work as expected.
func (*JSONRepository[T]) DropIndex ¶
func (r *JSONRepository[T]) DropIndex(ctx context.Context) error
DropIndex uses FT.DROPINDEX from the RediSearch module to drop index whose name is `jsonidx:{prefix}`
func (*JSONRepository[T]) Fetch ¶
func (r *JSONRepository[T]) Fetch(ctx context.Context, id string) (v *T, err error)
Fetch an entity whose name is `{prefix}:{id}`
func (*JSONRepository[T]) FetchCache ¶
func (r *JSONRepository[T]) FetchCache(ctx context.Context, id string, ttl time.Duration) (v *T, err error)
FetchCache is like Fetch, but it uses client side caching mechanism.
func (*JSONRepository[T]) IndexName ¶
func (r *JSONRepository[T]) IndexName() string
IndexName returns the index name used in the FT.CREATE
func (*JSONRepository[T]) NewEntity ¶
func (r *JSONRepository[T]) NewEntity() *T
NewEntity returns an empty entity and will have the `redis:",key"` field be set with ULID automatically.
func (*JSONRepository[T]) Remove ¶
func (r *JSONRepository[T]) Remove(ctx context.Context, id string) error
Remove the entity under the redis key of `{prefix}:{id}`.
func (*JSONRepository[T]) Save ¶
func (r *JSONRepository[T]) Save(ctx context.Context, entity *T) (err error)
Save the entity under the redis key of `{prefix}:{id}`. It also uses the `redis:",ver"` field and lua script to perform optimistic locking and prevent lost update.
func (*JSONRepository[T]) SaveMulti ¶
func (r *JSONRepository[T]) SaveMulti(ctx context.Context, entities ...*T) []error
SaveMulti batches multiple HashRepository.Save at once
func (*JSONRepository[T]) Search ¶
func (r *JSONRepository[T]) Search(ctx context.Context, cmdFn func(search FtSearchIndex) rueidis.Completed) (n int64, s []*T, err error)
Search uses FT.SEARCH from the RediSearch module to search the index whose name is `jsonidx:{prefix}` It returns three values: 1. total count of match results inside the redis, and note that it might be larger than returned search result. 2. search result, and note that its length might smaller than the first return value. 3. error if any You can use the cmdFn parameter to mutate the search command.
type Repository ¶
type Repository[T any] interface { NewEntity() (entity *T) Fetch(ctx context.Context, id string) (*T, error) FetchCache(ctx context.Context, id string, ttl time.Duration) (v *T, err error) Search(ctx context.Context, cmdFn func(search FtSearchIndex) rueidis.Completed) (int64, []*T, error) Aggregate(ctx context.Context, cmdFn func(agg FtAggregateIndex) rueidis.Completed) (*AggregateCursor, error) Save(ctx context.Context, entity *T) (err error) SaveMulti(ctx context.Context, entity ...*T) (errs []error) Remove(ctx context.Context, id string) error CreateIndex(ctx context.Context, cmdFn func(schema FtCreateSchema) rueidis.Completed) error DropIndex(ctx context.Context) error IndexName() string }
Repository is backed by HashRepository or JSONRepository
func NewHashRepository ¶
func NewHashRepository[T any](prefix string, schema T, client rueidis.Client, opts ...RepositoryOption) Repository[T]
NewHashRepository creates an HashRepository. The prefix parameter is used as redis key prefix. The entity stored by the repository will be named in the form of `{prefix}:{id}` The schema parameter should be a struct with fields tagged with `redis:",key"` and `redis:",ver"`
func NewJSONRepository ¶
func NewJSONRepository[T any](prefix string, schema T, client rueidis.Client, opts ...RepositoryOption) Repository[T]
NewJSONRepository creates an JSONRepository. The prefix parameter is used as redis key prefix. The entity stored by the repository will be named in the form of `{prefix}:{id}` The schema parameter should be a struct with fields tagged with `redis:",key"` and `redis:",ver"`
type RepositoryOption ¶
type RepositoryOption func(Repository[any])
func WithIndexName ¶
func WithIndexName(name string) RepositoryOption