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

Cache update #1572

Closed
wants to merge 3 commits into from
Closed
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
11 changes: 10 additions & 1 deletion server/config.docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ system:
oss-type: local # 控制oss选择走本地还是 七牛等其他仓 自行增加其他oss仓可以在 server/utils/upload/upload.go 中 NewOss函数配置
use-redis: false # 使用redis
use-mongo: false # 使用mongo
use-cache: false # 使用cache
use-multipoint: false
# IP限制次数 一个小时15000次
iplimit-count: 15000
Expand Down Expand Up @@ -217,4 +218,12 @@ cors:
allow-headers: content-type
allow-methods: GET, POST
expose-headers: Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type
allow-credentials: true # 布尔值
allow-credentials: true # 布尔值

# 缓存配置
cache:
mode: redis # 使用redis做缓存
redis:
db: 0
addr: 127.0.0.1:6379
password: ""
11 changes: 10 additions & 1 deletion server/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ system:
oss-type: local # 控制oss选择走本地还是 七牛等其他仓 自行增加其他oss仓可以在 server/utils/upload/upload.go 中 NewOss函数配置
use-redis: false # 使用redis
use-mongo: false # 使用mongo
use-cache: false # 使用cache
use-multipoint: false
# IP限制次数 一个小时15000次
iplimit-count: 15000
Expand Down Expand Up @@ -251,4 +252,12 @@ cors:
allow-headers: content-type
allow-methods: GET, POST
expose-headers: Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type
allow-credentials: true # 布尔值
allow-credentials: true # 布尔值

# 缓存配置
cache:
mode: redis # 使用redis做缓存
redis:
db: 0
addr: 127.0.0.1:6379
password: ""
6 changes: 6 additions & 0 deletions server/config/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package config

type Cache struct {
Mode string `mapstructure:"mode" json:"mode" yaml:"mode"`
Redis Redis
}
2 changes: 2 additions & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ type Server struct {

// 跨域配置
Cors CORS `mapstructure:"cors" json:"cors" yaml:"cors"`
// 缓存配置
Cache Cache `mapstructure:"cache" json:"cache" yaml:"cache"`
}
1 change: 1 addition & 0 deletions server/config/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ type System struct {
UseMultipoint bool `mapstructure:"use-multipoint" json:"use-multipoint" yaml:"use-multipoint"` // 多点登录拦截
UseRedis bool `mapstructure:"use-redis" json:"use-redis" yaml:"use-redis"` // 使用redis
UseMongo bool `mapstructure:"use-mongo" json:"use-mongo" yaml:"use-mongo"` // 使用redis
UseCache bool `mapstructure:"use-cache" json:"use-cache" yaml:"use-cache"` // 使用cache
}
21 changes: 21 additions & 0 deletions server/core/cache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cache

import (
"context"
"errors"
"time"
)

var ErrorKeyNotFound = errors.New("key not found")

type ICache interface {
Get(ctx context.Context, key string) (string, error)
Set(ctx context.Context, key, value string) error
SetEx(ctx context.Context, key, value string, expiration time.Duration) error
Exist(ctx context.Context, key string) (bool, error)
Delete(ctx context.Context, key string) error
Incr(ctx context.Context, key string) (int64, error)
Decr(ctx context.Context, key string) (int64, error)
Expire(ctx context.Context, key string, expiration time.Duration) (bool, error)
Ttl(ctx context.Context, key string) (int, error) // -1 永不过期, -2 没有找到key
}
38 changes: 38 additions & 0 deletions server/core/cache/cache_factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cache

import (
"context"
"errors"

"github.com/flipped-aurora/gin-vue-admin/server/config"
"github.com/redis/go-redis/v9"
)

func Create(c config.Cache) (ICache, error) {
if len(c.Mode) == 0 {
return nil, errors.New("mode not found")
}

switch c.Mode {
case "redis":
redisCfg := c.Redis
return createRedis(redisCfg.Addr, redisCfg.Password, redisCfg.DB)
default:
return nil, errors.New("mode not found")
}
}

func createRedis(addr, password string, db int) (ICache, error) {
opt := &redis.Options{
Addr: addr,
Password: password, // no password set
DB: db, // use default DB
}

client := redis.NewClient(opt)
if err := client.Ping(context.Background()).Err(); err != nil {
return nil, err
}

return NewCacheRedis(client), nil
}
77 changes: 77 additions & 0 deletions server/core/cache/cache_redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package cache

import (
"context"
"time"

"github.com/redis/go-redis/v9"
)

var _ ICache = (*CacheRedis)(nil)

type CacheRedis struct {
rds *redis.Client
}

func NewCacheRedis(client *redis.Client) *CacheRedis {
return &CacheRedis{
rds: client,
}
}

func (c *CacheRedis) Get(ctx context.Context, key string) (string, error) {
ret := c.rds.Get(ctx, key)
r, err := ret.Result()
switch {
case err == redis.Nil:
return "", ErrorKeyNotFound
case err != nil:
return "", err
}
return r, nil
}

func (c *CacheRedis) Set(ctx context.Context, key, value string) error {
return c.rds.Set(ctx, key, value, redis.KeepTTL).Err()
}

func (c *CacheRedis) SetEx(ctx context.Context, key, value string, expiration time.Duration) error {
return c.rds.SetEx(ctx, key, value, expiration).Err()
}

func (c *CacheRedis) Exist(ctx context.Context, key string) (bool, error) {
result, err := c.rds.Exists(ctx, key).Result()
if err != nil {
return false, err
}
return result == 1, nil
}

func (c *CacheRedis) Delete(ctx context.Context, key string) error {
return c.rds.Del(ctx, key).Err()
}

func (c *CacheRedis) Expire(ctx context.Context, key string, expiration time.Duration) (bool, error) {
return c.rds.Expire(ctx, key, expiration).Result()
}

func (c *CacheRedis) Ttl(ctx context.Context, key string) (int, error) {
result, err := c.rds.TTL(ctx, key).Result()
if err != nil {
return 0, err
}

if result <= 0 {
return int(result), nil
}

return int(result.Seconds()), nil
}

func (c *CacheRedis) Incr(ctx context.Context, key string) (int64, error) {
return c.rds.Incr(ctx, key).Result()
}

func (c *CacheRedis) Decr(ctx context.Context, key string) (int64, error) {
return c.rds.Decr(ctx, key).Result()
}
Loading