Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/v2.2.0 gredis #2155

Merged
merged 6 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 108 additions & 22 deletions database/gredis/gredis_redis_group_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package gredis

import (
"context"
"github.com/gogf/gf/v2/util/gconv"
)

type RedisGroupDB struct {
Expand All @@ -20,46 +21,131 @@ func (r *Redis) DB() *RedisGroupDB {
}
}

func (RedisGroupDB) Exists(ctx context.Context, keys ...string) (int64, error) {
panic("implement me")
// Exists return if key exists.
// The user should be aware that if the same existing key is mentioned in the arguments multiple times,
// it will be counted multiple times.
// So if somekey exists, EXISTS somekey somekey will return 2.
//
// https://redis.io/commands/exists/
func (r *RedisGroupDB) Exists(ctx context.Context, keys ...string) (int64, error) {
v, err := r.redis.Do(ctx, "EXISTS", keys)
return v.Int64(), err
}

func (RedisGroupDB) Type(ctx context.Context, key string) (string, error) {
panic("implement me")
// Type return the string representation of the type of the value stored at key.
// The different types that can be returned are: string, list, set, zset, hash and stream.
//
// https://redis.io/commands/type/
func (r *RedisGroupDB) Type(ctx context.Context, key string) (string, error) {
v, err := r.redis.Do(ctx, "TYPE", key)
return v.String(), err
}

func (RedisGroupDB) Rename(ctx context.Context, key, newKey string) (string, error) {
panic("implement me")
// Rename key to newkey. It return an error when key does not exist.
// If newkey already exists it is overwritten, when this happens RENAME executes an implicit DEL operation,
// so if the deleted key contains a very big value it may cause high latency even if RENAME itself is usually a constant-time operation.
//
// In Cluster mode, both key and newkey must be in the same hash slot,
// meaning that in practice only keys that have the same hash tag can be reliably renamed in cluster.
//
// https://redis.io/commands/rename/
func (r *RedisGroupDB) Rename(ctx context.Context, key, newKey string) (string, error) {
v, err := r.redis.Do(ctx, "RENAME", key, newKey)
return v.String(), err
}

func (RedisGroupDB) RenameNX(ctx context.Context, key, newKey string) (bool, error) {
panic("implement me")
// RenameNX renames key to newkey if newkey does not yet exist.
// It return an error when key does not exist.
// In Cluster mode, both key and newkey must be in the same hash slot,
// meaning that in practice only keys that have the same hash tag can be reliably renamed in cluster.
//
// https://redis.io/commands/renamenx/
func (r *RedisGroupDB) RenameNX(ctx context.Context, key, newKey string) (bool, error) {
v, err := r.redis.Do(ctx, "RENAME", key, newKey)
return v.Bool(), err
}

func (RedisGroupDB) Move(ctx context.Context, key, db string) (bool, error) {
panic("implement me")
// Move key from the currently selected database (see SELECT) to the specified destination database.
// When key already exists in the destination database, or it does not exist in the source database,
// it does nothing.
// It is possible to use MOVE as a locking primitive because of this.
//
// https://redis.io/commands/move/
func (r *RedisGroupDB) Move(ctx context.Context, key, db string) (bool, error) {
v, err := r.redis.Do(ctx, "MOVE", key, db)
return v.Bool(), err
}

func (RedisGroupDB) Del(ctx context.Context, keys ...string) (int64, error) {
panic("implement me")
// Del removes the specified keys.
// a key is ignored if it does not exist.
//
// https://redis.io/commands/del/
func (r *RedisGroupDB) Del(ctx context.Context, keys ...string) (int64, error) {
v, err := r.redis.Do(ctx, "DEL", keys)
return v.Int64(), err
}

func (RedisGroupDB) RandomKey(ctx context.Context) (string, error) {
panic("implement me")
// RandomKey return a random key from the currently selected database.
//
// https://redis.io/commands/randomkey/
func (r *RedisGroupDB) RandomKey(ctx context.Context) (string, error) {
v, err := r.redis.Do(ctx, "RANDOMKEY")
return v.String(), err
}

func (RedisGroupDB) DBSize(ctx context.Context) (int64, error) {
panic("implement me")
// DBSize return the number of keys in the currently-selected database.
//
// https://redis.io/commands/dbsize/
func (r *RedisGroupDB) DBSize(ctx context.Context) (int64, error) {
v, err := r.redis.Do(ctx, "DBSIZE")
return v.Int64(), err
}

func (RedisGroupDB) Keys(ctx context.Context, pattern string) ([]string, error) {
panic("implement me")
// Keys return all keys matching pattern.
// While the time complexity for this operation is O(N), the constant times are fairly low.
// For example, Redis running on an entry level laptop can scan a 1 million key database in 40 milliseconds.
// consider KEYS as a command that should only be used in production environments with extreme care.
// It may ruin performance when it is executed against large databases.
// This command is intended for debugging and special operations, such as changing your keyspace layout.
// Don't use KEYS in your regular application code.
// If you're looking for a way to find keys in a subset of your keyspace, consider using SCAN or sets.
//
// https://redis.io/commands/keys/
func (r *RedisGroupDB) Keys(ctx context.Context, pattern string) ([]string, error) {
v, err := r.redis.Do(ctx, "KEYS", pattern)
return gconv.SliceStr(v), err
}

func (RedisGroupDB) FlushDB(ctx context.Context) (string, error) {
panic("implement me")
// FlushDB delete all the keys of the currently selected DB. This command never fails.
//
// By default, FLUSHDB will synchronously flush all keys from the database.
// Starting with Redis 6.2, setting the lazyfree-lazy-user-flush configuration directive to "yes"
// changes the default flush mode to asynchronous.
// It is possible to use one of the following modifiers to dictate the flushing mode explicitly:
// ASYNC: flushes the database asynchronously
// SYNC: flushes the database synchronously
// Note: an asynchronous FLUSHDB command only deletes keys that were present at the time the command was invoked.
// Keys created during an asynchronous flush will be unaffected.
//
// https://redis.io/commands/flushdb/
func (r *RedisGroupDB) FlushDB(ctx context.Context, options string) error {
_, err := r.redis.Do(ctx, "FLUSHDB", options)
return err
}

func (RedisGroupDB) FlushAll(ctx context.Context) (string, error) {
panic("implement me")
// FlushAll delete all the keys of all the existing databases, not just the currently selected one.
// This command never fails.
// By default, FLUSHALL will synchronously flush all the databases.
// Starting with Redis 6.2, setting the lazyfree-lazy-user-flush configuration directive to "yes" changes the
// default flush mode to asynchronous.
// It is possible to use one of the following modifiers to dictate the flushing mode explicitly:
// ASYNC: flushes the databases asynchronously
// SYNC: flushes the databases synchronously
// Note: an asynchronous FLUSHALL command only deletes keys that were present at the time the command was invoked.
// Keys created during an asynchronous flush will be unaffected.
//
// https://redis.io/commands/flushall/
func (r *RedisGroupDB) FlushAll(ctx context.Context, options string) error {
_, err := r.redis.Do(ctx, "FLUSHALL", options)
return err
}
77 changes: 63 additions & 14 deletions database/gredis/gredis_redis_group_expire.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,79 @@ func (r *Redis) Expire() *RedisGroupExpire {
}
}

func (RedisGroupExpire) Expire(ctx context.Context, key string, seconds time.Duration) (bool, error) {
panic("implement me")
// Expire set a timeout on key.
// After the timeout has expired, the key will automatically be deleted.
//
// https://redis.io/commands/expire/
func (r *RedisGroupExpire) Expire(ctx context.Context, key string, seconds time.Duration) (bool, error) {
v, err := r.redis.Do(ctx, "EXPIRE", key, seconds.Seconds())
return v.Bool(), err
}

func (RedisGroupExpire) ExpireAt(ctx context.Context, key string, time time.Time) (bool, error) {
panic("implement me")
// ExpireAt has the same effect and semantic as EXPIRE, but instead of specifying the number of
// seconds representing the TTL (time to live), it takes an absolute Unix timestamp (seconds since
// January 1, 1970).
// A timestamp in the past will delete the key immediately.
//
// https://redis.io/commands/expireat/
func (r *RedisGroupExpire) ExpireAt(ctx context.Context, key string, time time.Time) (bool, error) {
v, err := r.redis.Do(ctx, "EXPIREAT", key, time)
return v.Bool(), err
}

func (RedisGroupExpire) TTL(ctx context.Context, key string) (time.Duration, error) {
panic("implement me")
// TTL return the remaining time to live of a key that has a timeout.
// This introspection capability allows a Redis client to check how many seconds a given key
// will continue to be part of the dataset.
// In Redis 2.6 or older the command returns -1 if the key does not exist or if the key exist but has
// no associated expire.
// Starting with Redis 2.8 the return value in case of error changed:
// The command returns -2 if the key does not exist.
// The command returns -1 if the key exists but has no associated expire.
// See also the PTTL command that returns the same information with milliseconds resolution (Only
// available in Redis 2.6 or greater).
//
// https://redis.io/commands/ttl/
func (r *RedisGroupExpire) TTL(ctx context.Context, key string) (int64, error) {
v, err := r.redis.Do(ctx, "TTL", key)
return v.Int64(), err
}

func (RedisGroupExpire) PErsist(ctx context.Context, key string, time time.Duration) (bool, error) {
panic("implement me")
// PErsist remove the existing timeout on key, turning the key from volatile (a key with an expire set)
// to persistent (a key that will never expire as no timeout is associated).
//
// https://redis.io/commands/persist/
func (r *RedisGroupExpire) PErsist(ctx context.Context, key string) (bool, error) {
v, err := r.redis.Do(ctx, "PERSIST", key)
return v.Bool(), err
}

func (RedisGroupExpire) PExpire(ctx context.Context, key string, time time.Duration) (bool, error) {
panic("implement me")
// PExpire works exactly like EXPIRE but the time to live of the key is specified in milliseconds
// instead of seconds.
//
// https://redis.io/commands/pexpire/
func (r *RedisGroupExpire) PExpire(ctx context.Context, key string, time time.Duration, options string) (bool, error) {
v, err := r.redis.Do(ctx, "PEXPIRE", key, time.Milliseconds(), options)
return v.Bool(), err
}

func (RedisGroupExpire) PExpireAt(ctx context.Context, key string, time time.Time) (bool, error) {
panic("implement me")
// PExpireAt has the same effect and semantic as EXPIREAT, but the Unix time at which the key will
// expire is specified in milliseconds instead of seconds.
//
// https://redis.io/commands/pexpireat/
func (r *RedisGroupExpire) PExpireAt(ctx context.Context, key string, time time.Time) (bool, error) {
v, err := r.redis.Do(ctx, "PEXPIREAT", key, time)
return v.Bool(), err
}

func (RedisGroupExpire) PTTL(ctx context.Context, key string) (time.Duration, error) {
panic("implement me")
// PTTL like TTL this command returns the remaining time to live of a key that has an expire set,
// with the sole difference that TTL returns the amount of remaining time in seconds while PTTL
// returns it in milliseconds.
//
// In Redis 2.6 or older the command returns -1 if the key does not exist or if the key exist but has
// no associated expire.
//
// https://redis.io/commands/pttl/
func (r *RedisGroupExpire) PTTL(ctx context.Context, key string) (int64, error) {
v, err := r.redis.Do(ctx, "PTTL", key)
return v.Int64(), err
}
Loading