优化结构
This commit is contained in:
Vendored
+32
@@ -87,6 +87,38 @@ func (s *Store) Delete(ctx context.Context, key string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) GetAndDelete(ctx context.Context, key string) (string, bool, error) {
|
||||
if client := s.client(); client != nil {
|
||||
value, err := client.Eval(ctx, "local v=redis.call('GET',KEYS[1]); if v then redis.call('DEL',KEYS[1]); end; return v", []string{key}).Result()
|
||||
if err == redis.Nil {
|
||||
return "", false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
if value == nil {
|
||||
return "", false, nil
|
||||
}
|
||||
switch typed := value.(type) {
|
||||
case string:
|
||||
return typed, true, nil
|
||||
case []byte:
|
||||
return string(typed), true, nil
|
||||
default:
|
||||
return "", false, nil
|
||||
}
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
entry, ok := s.memory[key]
|
||||
if !ok || (!entry.expiresAt.IsZero() && time.Now().After(entry.expiresAt)) {
|
||||
delete(s.memory, key)
|
||||
return "", false, nil
|
||||
}
|
||||
delete(s.memory, key)
|
||||
return entry.value, true, nil
|
||||
}
|
||||
|
||||
func (s *Store) Increment(ctx context.Context, key string, expiration time.Duration) (int64, error) {
|
||||
if client := s.client(); client != nil {
|
||||
if expiration > 0 {
|
||||
|
||||
Reference in New Issue
Block a user