96 lines
2.5 KiB
Go
96 lines
2.5 KiB
Go
package redis
|
|
|
|
import (
|
|
"github.com/go-redis/redis"
|
|
"time"
|
|
)
|
|
|
|
// List represents a redis list.
|
|
type List struct {
|
|
key string
|
|
client *redis.Client
|
|
}
|
|
|
|
// NewList creates a new redis list instance.
|
|
func NewList(client *redis.Client, key string) *List {
|
|
return &List{
|
|
key: key,
|
|
client: client,
|
|
}
|
|
}
|
|
|
|
// Key returns the key of the list.
|
|
func (l *List) Key() string {
|
|
return l.key
|
|
}
|
|
|
|
// Expire sets the expiration time of the list.
|
|
func (l *List) Expire(expiration time.Duration) (bool, error) {
|
|
return l.client.Expire(l.key, expiration).Result()
|
|
}
|
|
|
|
// Delete deletes the list.
|
|
func (l *List) Delete() (int64, error) {
|
|
return l.client.Del(l.key).Result()
|
|
}
|
|
|
|
// Len returns the length of the list.
|
|
func (l *List) Len() (int64, error) {
|
|
return l.client.LLen(l.key).Result()
|
|
}
|
|
|
|
// Push adds a new element to the tail of the list.
|
|
func (l *List) Push(value interface{}) error {
|
|
return l.client.RPush(l.key, value).Err()
|
|
}
|
|
|
|
// Pop removes and returns the first element of the list.
|
|
func (l *List) Pop() (string, error) {
|
|
return l.client.LPop(l.key).Result()
|
|
}
|
|
|
|
// PushFront adds a new element to the head of the list.
|
|
func (l *List) PushFront(value interface{}) error {
|
|
return l.client.LPush(l.key, value).Err()
|
|
}
|
|
|
|
// PopBack removes and returns the last element of the list.
|
|
func (l *List) PopBack() (string, error) {
|
|
return l.client.RPop(l.key).Result()
|
|
}
|
|
|
|
// Index returns the element at the specified index.
|
|
func (l *List) Index(index int64) (string, error) {
|
|
return l.client.LIndex(l.key, index).Result()
|
|
}
|
|
|
|
// Range returns a range of elements from the list.
|
|
func (l *List) Range(start, stop int64) ([]string, error) {
|
|
return l.client.LRange(l.key, start, stop).Result()
|
|
}
|
|
|
|
// Trim removes elements from the list.
|
|
func (l *List) Trim(start, stop int64) error {
|
|
return l.client.LTrim(l.key, start, stop).Err()
|
|
}
|
|
|
|
// Set sets the element at the specified index.
|
|
func (l *List) Set(index int64, value interface{}) error {
|
|
return l.client.LSet(l.key, index, value).Err()
|
|
}
|
|
|
|
// InsertBefore inserts a new element before the specified element.
|
|
func (l *List) InsertBefore(pivot, value interface{}) error {
|
|
return l.client.LInsertBefore(l.key, pivot, value).Err()
|
|
}
|
|
|
|
// InsertAfter inserts a new element after the specified element.
|
|
func (l *List) InsertAfter(pivot, value interface{}) error {
|
|
return l.client.LInsertAfter(l.key, pivot, value).Err()
|
|
}
|
|
|
|
// Remove removes all occurrences of the specified element.
|
|
func (l *List) Remove(count int64, value interface{}) (int64, error) {
|
|
return l.client.LRem(l.key, count, value).Result()
|
|
}
|