Browse Source

update package

fancl 1 year ago
parent
commit
a995627a52
6 changed files with 78 additions and 1 deletions
  1. 13 0
      entry/http/error.go
  2. 3 0
      instance.go
  3. 5 0
      pkg/cache/cache.go
  4. 4 0
      pkg/cache/instance.go
  5. 37 1
      pkg/cache/memcache.go
  6. 16 0
      util/reflection/reflection.go

+ 13 - 0
entry/http/error.go

@@ -0,0 +1,13 @@
+package http
+
+const (
+	ErrAccessDenied         = 4003 //拒绝访问
+	ErrPermissionDenied     = 4004 //没有权限
+	ErrInvalidRequest       = 4005 //请求无效或不合法
+	ErrInvalidPayload       = 4006 //请求数据无效
+	ErrResourceCreate       = 8001 //资源创建失败
+	ErrResourceUpdate       = 8002 //资源更新失败
+	ErrResourceDelete       = 8003 //资源删除失败
+	ErrResourceNotFound     = 8004 //资源未找到
+	ErrTemporaryUnavailable = 8006 //临时性不可用
+)

+ 3 - 0
instance.go

@@ -3,6 +3,9 @@ package kos
 import (
 import (
 	"git.nspix.com/golang/kos/entry/cli"
 	"git.nspix.com/golang/kos/entry/cli"
 	"git.nspix.com/golang/kos/entry/http"
 	"git.nspix.com/golang/kos/entry/http"
+	_ "git.nspix.com/golang/kos/util/fetch"
+	_ "git.nspix.com/golang/kos/util/random"
+	_ "git.nspix.com/golang/kos/util/reflection"
 	"sync"
 	"sync"
 )
 )
 
 

+ 5 - 0
pkg/cache/cache.go

@@ -5,9 +5,14 @@ import (
 	"time"
 	"time"
 )
 )
 
 
+type (
+	LoadFunc func(ctx context.Context) (any, error)
+)
+
 type Cache interface {
 type Cache interface {
 	Set(ctx context.Context, key string, value any)
 	Set(ctx context.Context, key string, value any)
 	SetEx(ctx context.Context, key string, value any, expire time.Duration)
 	SetEx(ctx context.Context, key string, value any, expire time.Duration)
 	Get(ctx context.Context, key string) (value any, ok bool)
 	Get(ctx context.Context, key string) (value any, ok bool)
+	Try(ctx context.Context, key string, cb LoadFunc) (value any, err error)
 	Del(ctx context.Context, key string)
 	Del(ctx context.Context, key string)
 }
 }

+ 4 - 0
pkg/cache/instance.go

@@ -29,6 +29,10 @@ func SetEx(ctx context.Context, key string, value any, expire time.Duration) {
 	std.SetEx(ctx, key, value, expire)
 	std.SetEx(ctx, key, value, expire)
 }
 }
 
 
+func Try(ctx context.Context, key string, cb LoadFunc) (value any, err error) {
+	return std.Try(ctx, key, cb)
+}
+
 func Get(ctx context.Context, key string) (value any, ok bool) {
 func Get(ctx context.Context, key string) (value any, ok bool) {
 	return std.Get(ctx, key)
 	return std.Get(ctx, key)
 }
 }

+ 37 - 1
pkg/cache/memcache.go

@@ -2,14 +2,50 @@ package cache
 
 
 import (
 import (
 	"context"
 	"context"
+	"git.nspix.com/golang/kos/util/env"
 	"github.com/patrickmn/go-cache"
 	"github.com/patrickmn/go-cache"
+	"os"
 	"time"
 	"time"
 )
 )
 
 
+var (
+	memCacheDefaultExpired  time.Duration
+	memCacheCleanupInterval time.Duration
+)
+
+func init() {
+	memCacheDefaultExpired, _ = time.ParseDuration(env.Get("MEMCACHE_DEFAULT_EXPIRED", "1h"))
+	memCacheCleanupInterval, _ = time.ParseDuration(env.Get("MEMCACHE_CLEANUP_INTERVAL", "10m"))
+
+	if memCacheDefaultExpired < time.Second*5 {
+		memCacheDefaultExpired = time.Second * 5
+	}
+
+	if memCacheCleanupInterval < time.Minute {
+		memCacheCleanupInterval = time.Minute
+	}
+}
+
 type MemCache struct {
 type MemCache struct {
 	engine *cache.Cache
 	engine *cache.Cache
 }
 }
 
 
+func (cache *MemCache) Try(ctx context.Context, key string, cb LoadFunc) (value any, err error) {
+	var (
+		ok bool
+	)
+	if value, ok = cache.engine.Get(key); ok {
+		return value, nil
+	}
+	if cb == nil {
+		return nil, os.ErrNotExist
+	}
+	if value, err = cb(ctx); err == nil {
+		cache.engine.Set(key, value, 0)
+	}
+	return
+}
+
 func (cache *MemCache) Set(ctx context.Context, key string, value any) {
 func (cache *MemCache) Set(ctx context.Context, key string, value any) {
 	cache.engine.Set(key, value, 0)
 	cache.engine.Set(key, value, 0)
 }
 }
@@ -28,6 +64,6 @@ func (cache *MemCache) Del(ctx context.Context, key string) {
 
 
 func NewMemCache() *MemCache {
 func NewMemCache() *MemCache {
 	return &MemCache{
 	return &MemCache{
-		engine: cache.New(time.Hour, time.Minute*10),
+		engine: cache.New(memCacheDefaultExpired, memCacheCleanupInterval),
 	}
 	}
 }
 }

+ 16 - 0
util/reflection/reflection.go

@@ -0,0 +1,16 @@
+package reflection
+
+import "git.nspix.com/golang/kos/util/reflect"
+
+func Setter(hacky any, variables map[string]any) (err error) {
+	for k, v := range variables {
+		if err = Set(hacky, k, v); err != nil {
+			return err
+		}
+	}
+	return
+}
+
+func Set(hacky any, field string, value interface{}) (err error) {
+	return reflect.Set(hacky, field, value)
+}