|
- // Copyright © 2023 OpenIM open source community. All rights reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
-
- package cache
-
- import (
- "context"
- "time"
-
- "github.com/go-redis/redis/v8"
- "github.com/pkg/errors"
- )
-
- var _ Cache = new(cache)
-
- // Cache cache interface.
- type Cache interface {
- Set(ctx context.Context, key, value string, ttl time.Duration) error
- Get(ctx context.Context, key string) (string, error)
- TTL(ctx context.Context, key string) (time.Duration, error)
- Expire(ctx context.Context, key string, ttl time.Duration) bool
- ExpireAt(ctx context.Context, key string, ttl time.Time) bool
- Del(ctx context.Context, key string) bool
- Exists(ctx context.Context, keys ...string) bool
- Incr(ctx context.Context, key string) int64
- Close() error
- }
-
- // cache cache dao representative.
- type cache struct {
- client *redis.Client
- }
-
- // Use return cache db.
- func Use(client *redis.Client) Cache {
- return &cache{
- client: client,
- }
- }
-
- // Get get value.
- func (c *cache) Get(ctx context.Context, key string) (string, error) {
- value, err := c.client.Get(ctx, key).Result()
- if err != nil {
- return "", errors.Wrapf(err, "redis get key: %s err", key)
- }
-
- return value, nil
- }
-
- // Set set key value pair to redis.
- func (c *cache) Set(ctx context.Context, key string, value string, ttl time.Duration) error {
- if err := c.client.Set(ctx, key, value, ttl).Err(); err != nil {
- return errors.Wrapf(err, "redis set key: %s err", key)
- }
-
- return nil
- }
-
- // TTL get ttl of key.
- func (c *cache) TTL(ctx context.Context, key string) (time.Duration, error) {
- ttl, err := c.client.TTL(ctx, key).Result()
- if err != nil {
- return -1, errors.Wrapf(err, "redis get key: %s err", key)
- }
-
- return ttl, nil
- }
-
- // Expire expire key.
- func (c *cache) Expire(ctx context.Context, key string, ttl time.Duration) bool {
- ok, _ := c.client.Expire(ctx, key, ttl).Result()
-
- return ok
- }
-
- // ExpireAt expire key at time.
- func (c *cache) ExpireAt(ctx context.Context, key string, ttl time.Time) bool {
- ok, _ := c.client.ExpireAt(ctx, key, ttl).Result()
-
- return ok
- }
-
- // Exists check some keys are exist.
- func (c *cache) Exists(ctx context.Context, keys ...string) bool {
- if len(keys) == 0 {
- return true
- }
- value, _ := c.client.Exists(ctx, keys...).Result()
-
- return value > 0
- }
-
- // Del delete key.
- func (c *cache) Del(ctx context.Context, key string) bool {
- if key == "" {
- return true
- }
-
- value, _ := c.client.Del(ctx, key).Result()
-
- return value > 0
- }
-
- // Incr incr key.
- func (c *cache) Incr(ctx context.Context, key string) int64 {
- value, _ := c.client.Incr(ctx, key).Result()
-
- return value
- }
-
- // Close close redis client.
- func (c *cache) Close() error {
- return c.client.Close()
- }
|