Browse Source

添加缓存文档

yjp 1 year ago
parent
commit
c32a27124f
2 changed files with 64 additions and 5 deletions
  1. 11 5
      README.md
  2. 53 0
      framework/core/infrastructure/cache/cache.go

+ 11 - 5
README.md

@@ -35,7 +35,7 @@ import (
 func main() {
 	app := baize.NewApplication(application.Config{
 		ApiConfig: application.ApiConfig{
-			UrlPrefix: "test",
+			UrlPrefix: "service",
 			Port:      "10100",
 		},
 	})
@@ -50,6 +50,9 @@ func main() {
 			fmt.Println("Global Before2")
 			c.Next()
 			fmt.Println("Global After2")
+		}).
+		AddRoute(http.MethodGet, "/route", func(c *api.Context) {
+			fmt.Println("By Add Route")
 		})
 
 	rootBinder := binding.NewBinder(app.ChooseRouter(api.RouterRoot, ""), app.Infrastructure())
@@ -61,11 +64,11 @@ func main() {
 			c.String(http.StatusOK, "pong")
 			return nil, nil
 		},
-	}, func(c *api.Context) {
+	}, func(c *api.Context, i *infrastructure.Infrastructure) {
 		fmt.Println("Root Route Before1")
 		c.Next()
 		fmt.Println("Root Route After1")
-	}, func(c *api.Context) {
+	}, func(c *api.Context, i *infrastructure.Infrastructure) {
 		fmt.Println("Root Route Before2")
 		c.Next()
 		fmt.Println("Root Route After2")
@@ -81,6 +84,9 @@ func main() {
 			fmt.Println("Global Before2")
 			c.Next()
 			fmt.Println("Global After2")
+		}).
+		AddRoute(http.MethodGet, "/route", func(c *api.Context) {
+			fmt.Println("By Add Route")
 		})
 
 	prefixRootBinder := binding.NewBinder(app.ChooseRouter(api.RouterPrefix, "v1"), app.Infrastructure())
@@ -92,11 +98,11 @@ func main() {
 			c.String(http.StatusOK, "pong")
 			return nil, nil
 		},
-	}, func(c *api.Context) {
+	}, func(c *api.Context, i *infrastructure.Infrastructure) {
 		fmt.Println("Versioned Route Before1")
 		c.Next()
 		fmt.Println("Versioned Route After1")
-	}, func(c *api.Context) {
+	}, func(c *api.Context, i *infrastructure.Infrastructure) {
 		fmt.Println("Versioned Route Before2")
 		c.Next()
 		fmt.Println("Versioned Route After2")

+ 53 - 0
framework/core/infrastructure/cache/cache.go

@@ -11,15 +11,35 @@ import (
 	"time"
 )
 
+// Cache 缓存基础设施接口
 type Cache interface {
+	// Set 设置缓存值
 	Set(key string, value string, expireSec int64) error
+
+	// Get 获取缓存值
 	Get(key string) (string, error)
+
+	// GetMulti 获取多个缓存值
 	GetMulti(keys []string) (map[string]string, error)
+
+	// GetAll 获取所有缓存值
 	GetAll() (map[string]string, error)
+
+	// Delete 删除缓存值
 	Delete(key string) error
+
+	// Clear 清除缓存
 	Clear() error
 }
 
+// Set 设置缓存值
+// 参数:
+// - cache: 缓存基础设施接口
+// - key: 缓存的键
+// - value: 要设置的缓存值
+// - expireSec: 缓存过期时间,单位秒
+// 返回值:
+// - 错误
 func Set(cache Cache, key string, value any, expireSec int64) error {
 	valueReflectValue := reflect.ValueOf(value)
 
@@ -35,6 +55,15 @@ func Set(cache Cache, key string, value any, expireSec int64) error {
 	return cache.Set(key, stringValue, expireSec)
 }
 
+// Get 获取缓存值
+// 泛型参数:
+// - T: 缓存值类型
+// 参数:
+// - cache: 缓存基础设施接口
+// - key: 缓存的键
+// 返回值:
+// - 缓存值
+// - 错误
 func Get[T any](cache Cache, key string) (T, error) {
 	var zero T
 
@@ -61,18 +90,42 @@ func Get[T any](cache Cache, key string) (T, error) {
 	return retValue, nil
 }
 
+// GetMulti 批量获取缓存值
+// 参数:
+// - cache: 缓存基础设施接口
+// - keys: 缓存的键(多个)
+// 返回值:
+// - 缓存值,value以string类型先返回
+// - 错误
 func GetMulti(cache Cache, keys []string) (map[string]string, error) {
 	return cache.GetMulti(keys)
 }
 
+// GetAll 获取所有缓存值
+// 参数:
+// - cache: 缓存基础设施接口
+// 返回值:
+// - 缓存值,value以string类型先返回
+// - 错误
 func GetAll(cache Cache) (map[string]string, error) {
 	return cache.GetAll()
 }
 
+// Delete 删除缓存值
+// 参数:
+// - cache: 缓存基础设施接口
+// - key: 缓存的键
+// 返回值:
+// - 错误
 func Delete(cache Cache, key string) error {
 	return cache.Delete(key)
 }
 
+// Clear 清除缓存
+// 参数:
+// - cache: 缓存基础设施接口
+// 返回值:
+// - 错误
 func Clear(cache Cache) error {
 	return cache.Clear()
 }