Documentation
¶
Index ¶
- Constants
- Variables
- func Paginate(ctx context.Context, pageFn PageFunc, from, to *time.Time, offset, limit int) (iter.Seq2[[]byte, error], error)
- type BulkSetRecord
- type PageFunc
- type RedisTKV
- func (r *RedisTKV) BulkSet(ctx context.Context, records []BulkSetRecord) error
- func (r *RedisTKV) Delete(ctx context.Context, id ...string) error
- func (r *RedisTKV) Exists(ctx context.Context, id ...string) (bool, error)
- func (r *RedisTKV) FetchPage(ctx context.Context, from, to *time.Time, offset, limit int) (iter.Seq2[[]byte, error], int64, error)
- func (r *RedisTKV) FetchPageConsistent(ctx context.Context, from, to *time.Time, offset, limit int) (iter.Seq2[[]byte, error], int64, error)
- func (r *RedisTKV) Get(ctx context.Context, id ...string) ([]byte, error)
- func (r *RedisTKV) Set(ctx context.Context, data []byte, lastModified time.Time, id ...string) (bool, error)
Examples ¶
Constants ¶
View Source
const ( // DelimUnit is ASCII unit separator character. // Use this as a delimeter for keys if you need // a non-printable character. Safest choice. DelimUnit = "\x1f" // DelimPipe is ASCII pipe character. // Use this as a separator in keys if you need // a printable character. May be easier to debug. DelimPipe = "|" )
Variables ¶
View Source
var ErrUnexpectedScriptResult = errors.New("unexpected result from lua script")
Functions ¶
Types ¶
type RedisTKV ¶
type RedisTKV struct {
// contains filtered or unexported fields
}
RedisTKV is a k/v store backed by Redis. It uses a sorted set to keep track of last modified time and enable range queries.
Example ¶
ctx := context.Background()
store := rtkv.NewRedisTKV(rtkv.DelimUnit, "example", newGoRedisClient(0))
now := time.Now()
// Set the value of entity "a", "a"
existed, err := store.Set(ctx, []byte(`{"id": "a"}`), now, "a", "a")
fmt.Println(existed, err)
// Get the value of id "a" ([]byte(nil))
val, err := store.Get(ctx, "a")
fmt.Printf("%#v %v\n", val, err)
// Get the value of id "a", "a" ({"id": "a"}, <nil>)
val, err = store.Get(ctx, "a", "a")
fmt.Printf("%s %v\n", val, err)
// Bulk set some entities
_ = store.BulkSet(ctx, []rtkv.BulkSetRecord{
{Data: []byte(`{"id": "b"}`), ID: []string{"a", "b", "b"}, LastModified: now.Add(-time.Minute)},
{Data: []byte(`{"id": "c"}`), ID: []string{"a", "b", "c"}, LastModified: now.Add(-2 * time.Minute)},
{Data: []byte(`{"id": "d"}`), ID: []string{"a", "b", "d"}, LastModified: now.Add(-3 * time.Minute)},
{Data: []byte(`{"id": "e"}`), ID: []string{"a", "b", "e"}, LastModified: now.Add(-4 * time.Hour)},
})
// Get max 2 entities from a range that matches 3 in a set of 5 (oldest first)
from := now.Add(-3 * time.Minute)
to := now.Add(-time.Minute)
iterator, total, err := store.FetchPage(ctx, &from, &to, 0, 2)
fmt.Println(
"err:", err,
"total:", total,
)
for data, err := range iterator {
fmt.Println(string(data), err)
}
Output: false <nil> []byte(nil) <nil> {"id": "a"} <nil> err: <nil> total: 3 {"id": "d"} <nil> {"id": "c"} <nil>
func NewRedisTKV ¶
NewRedisTKV creates a new RedisTKV instance. The namespace is used to prefix keys in Redis.
The `idDelimiter` argument is used as a namespace delimeter and to pack composite IDs into a single key.
The `namespace` argument prevents key collisions for different entitiy types.
func (*RedisTKV) BulkSet ¶
func (r *RedisTKV) BulkSet(ctx context.Context, records []BulkSetRecord) error
BulkSet sets multiple entities in the store.
func (*RedisTKV) FetchPageConsistent ¶
Click to show internal directories.
Click to hide internal directories.