init project
This commit is contained in:
commit
d63cc8d9f5
33 files changed
+1529
No files matched your search
Vendored
+146
@@ -0,0 +1,146 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"git.xiaotuo.co/micro/skeleton/internal/logger"
|
||||
"github.com/go-redis/redis"
|
||||
)
|
||||
|
||||
type BaseCache struct {
|
||||
rs *redis.Client
|
||||
isLog bool
|
||||
}
|
||||
|
||||
func (c *BaseCache) exists(ctx context.Context, key string) (bool, error) {
|
||||
value, err := c.rs.Exists(key).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
if c.isLog {
|
||||
logger.Debugf(ctx, "redis_exists", "key: %s, not_found", key)
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
if c.isLog {
|
||||
logger.Errorf(ctx, "redis_exists", "key: %s, err: %v", key, err)
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
exists := value > 0
|
||||
if c.isLog {
|
||||
logger.Debugf(ctx, "redis_exists", "key: %s, exists", key)
|
||||
}
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
func (c *BaseCache) get(ctx context.Context, key string) (string, error) {
|
||||
value, err := c.rs.Get(key).Result()
|
||||
if err != nil {
|
||||
if c.isLog {
|
||||
logger.Errorf(ctx, "redis_get", "key: %s, err: %v", key, err)
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
if c.isLog {
|
||||
logger.Debugf(ctx, "redis_get", "key: %s, value: %s", key, value)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func (c *BaseCache) set(ctx context.Context, key string, value interface{}, expire time.Duration) (string, error) {
|
||||
result, err := c.rs.Set(key, value, expire).Result()
|
||||
if err != nil {
|
||||
if c.isLog {
|
||||
logger.Errorf(ctx, "redis_set", "key: %s, value: %v, err: %v", key, value, err)
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
if c.isLog {
|
||||
logger.Debugf(ctx, "redis_set", "key: %s, value: %v", key, value)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (c *BaseCache) del(ctx context.Context, key string) (int64, error) {
|
||||
affect, err := c.rs.Del(key).Result()
|
||||
if err != nil {
|
||||
if c.isLog {
|
||||
logger.Errorf(ctx, "redis_del", "key: %s, err: %v", key, err)
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
if c.isLog {
|
||||
logger.Debugf(ctx, "redis_del", "key: %s, affect: %v", key, affect)
|
||||
}
|
||||
return affect, nil
|
||||
}
|
||||
|
||||
func (c *BaseCache) push(ctx context.Context, key string, values ...interface{}) (int64, error) {
|
||||
affect, err := c.rs.LPush(key, values...).Result()
|
||||
if err != nil {
|
||||
if c.isLog {
|
||||
logger.Errorf(ctx, "redis_lpush", "key: %s, err: %v", key, err)
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
if c.isLog {
|
||||
logger.Debugf(ctx, "redis_lpush", "key: %s, values: %v, affect: %v", key, values, affect)
|
||||
}
|
||||
return affect, nil
|
||||
}
|
||||
|
||||
func (c *BaseCache) pop(ctx context.Context, key string) (string, error) {
|
||||
value, err := c.rs.RPop(key).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
if c.isLog {
|
||||
logger.Tracef(ctx, "redis_rpop", "key: %s null", key)
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
if err != nil {
|
||||
if c.isLog {
|
||||
logger.Errorf(ctx, "redis_rpop", "key: %s, err: %v", key, err)
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
if c.isLog {
|
||||
logger.Debugf(ctx, "redis_rpop", "key: %s, value: %v", key, value)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func (c *BaseCache) length(ctx context.Context, key string) (int64, error) {
|
||||
l, err := c.rs.LLen(key).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
if c.isLog {
|
||||
logger.Debugf(ctx, "redis_llen", "key: %s null", key)
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
if err != nil {
|
||||
if c.isLog {
|
||||
logger.Errorf(ctx, "redis_llen", "key: %s, err: %v", key, err)
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
if c.isLog {
|
||||
logger.Debugf(ctx, "redis_llen", "key: %s, length: %v", key, l)
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
|
||||
func (c *BaseCache) lock(ctx context.Context, key, value string, expire time.Duration) (bool, error) {
|
||||
result, err := c.rs.SetNX(key, value, expire).Result()
|
||||
if err != nil {
|
||||
if c.isLog {
|
||||
logger.Errorf(ctx, "redis_setnx", "key: %s, err: %v", key, err)
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
if c.isLog {
|
||||
logger.Debugf(ctx, "redis_setnx", "key: %s, result: %t", key, result)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
package cache
|
||||
@@ -0,0 +1,21 @@
|
||||
package storage
|
||||
|
||||
type Config struct {
|
||||
Mysql ConfigMysql `json:"mysql"`
|
||||
Redis ConfigRedis `json:"redis"`
|
||||
}
|
||||
|
||||
type ConfigMysql struct {
|
||||
DB string `json:"db"`
|
||||
Debug bool `json:"debug"`
|
||||
ShowSQL bool `json:"show_sql"`
|
||||
MaxOpenConn int `json:"max_open_conn"`
|
||||
MaxIdleConn int `json:"max_idle_conn"`
|
||||
ConnMaxLifetime int `json:"conn_max_life_time"`
|
||||
}
|
||||
|
||||
type ConfigRedis struct {
|
||||
Addr string `json:"addr"`
|
||||
Auth string `json:"auth"`
|
||||
DB int `json:"db"`
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package model
|
||||
|
||||
type BaseModel struct {
|
||||
Id int64 `json:"id" xorm:"pk autoincr 'id'"`
|
||||
CreatedAt int64 `json:"created_at" xorm:"created"`
|
||||
UpdatedAt int64 `json:"updated_at" xorm:"updated"`
|
||||
DeletedAt int64 `json:"deleted_at" xorm:"deleted notnull"`
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis"
|
||||
|
||||
xormxlog "git.xiaotuo.co/pkg/go-plugins/xormx/log"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
var (
|
||||
DefaultMaxOpenConn = 10
|
||||
DefaultMaxIdleConn = 5
|
||||
DefaultConnMaxLifetime = 1
|
||||
)
|
||||
|
||||
type Storage struct {
|
||||
engine *xorm.Engine
|
||||
}
|
||||
|
||||
func NewStorage(conf Config) (*Storage, error) {
|
||||
rs := redis.NewClient(&redis.Options{
|
||||
Addr: conf.Redis.Addr,
|
||||
Password: conf.Redis.Auth,
|
||||
DB: conf.Redis.DB,
|
||||
})
|
||||
if _, err := rs.Ping().Result(); err != nil {
|
||||
return nil, fmt.Errorf("redis.Ping: %v", err)
|
||||
}
|
||||
log.Infof("redis addr: %s, db: %d", conf.Redis.Addr, conf.Redis.DB)
|
||||
|
||||
if len(conf.Mysql.DB) == 0 {
|
||||
return nil, errors.New("db init failed: 数据库配置项为空")
|
||||
}
|
||||
if conf.Mysql.MaxIdleConn == 0 {
|
||||
conf.Mysql.MaxIdleConn = DefaultMaxIdleConn
|
||||
}
|
||||
if conf.Mysql.MaxOpenConn == 0 {
|
||||
conf.Mysql.MaxOpenConn = DefaultMaxOpenConn
|
||||
}
|
||||
if conf.Mysql.ConnMaxLifetime == 0 {
|
||||
conf.Mysql.ConnMaxLifetime = DefaultConnMaxLifetime
|
||||
}
|
||||
engine, err := xorm.NewEngine("mysql", conf.Mysql.DB)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("NewEngine: %v", err)
|
||||
}
|
||||
log.Infof("mysql addr: %s", xormxlog.SafeAddr(conf.Mysql.DB))
|
||||
|
||||
engine.SetMaxOpenConns(conf.Mysql.MaxOpenConn)
|
||||
engine.SetMaxIdleConns(conf.Mysql.MaxIdleConn)
|
||||
engine.SetConnMaxLifetime(time.Hour * time.Duration(conf.Mysql.ConnMaxLifetime))
|
||||
|
||||
if conf.Mysql.ShowSQL {
|
||||
engine.SetLogger(xormxlog.NewXormxLogger())
|
||||
engine.ShowSQL(true)
|
||||
}
|
||||
// redisLog := conf.Mysql.ShowSQL
|
||||
|
||||
return &Storage{
|
||||
engine: engine,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Storage) NewSession() *xorm.Session {
|
||||
return s.engine.NewSession()
|
||||
}
|
||||
|
||||
type TransactionClosure func(tx *xorm.Session) error
|
||||
Reference in new issue
Block a user