3 Commits 6d9ec7b5aa ... c28ba06818

Author SHA1 Message Date
  runningwater c28ba06818 feat(Command): cache forget 命令 3 months ago
  runningwater fc98160c9f feat(Command): cache clear 命令 3 months ago
  runningwater 7cc0a0542c feat: 缓存友情链接列表 3 months ago
5 changed files with 78 additions and 1 deletions
  1. 5 0
      README.md
  2. 50 0
      app/cmd/cache.go
  3. 1 1
      app/http/controllers/api/v1/links_controller.go
  4. 20 0
      app/models/link/link_util.go
  5. 2 0
      main.go

+ 5 - 0
README.md

@@ -76,6 +76,11 @@ UNIQUE KEY `migration` (`migration`)
 
 #### 🚀 新功能
 
+- *(Command)* Cache forget 命令
+- *(Command)* Cache clear 命令
+- 缓存友情链接列表
+- Cache 包
+- 友情链接列表
 - 显示话题
 - 话题列表
 - 删除话题

+ 50 - 0
app/cmd/cache.go

@@ -0,0 +1,50 @@
+package cmd
+
+import (
+	"fmt"
+
+	"github.com/spf13/cobra"
+
+	"github.com/runningwater/gohub/pkg/cache"
+	"github.com/runningwater/gohub/pkg/console"
+)
+
+var CmdCache = &cobra.Command{
+	Use:   "cache",
+	Short: "Cache Management",
+}
+
+var CmdCacheClear = &cobra.Command{
+	Use:   "clear",
+	Short: "Clear Cache",
+	Run:   runCacheClear,
+}
+
+var CmdCacheForget = &cobra.Command{
+	Use:   "forget",
+	Short: "Delete redis key, example: gohub cache forget key",
+	Run:   runCacheForget,
+}
+
+func runCacheClear(cmd *cobra.Command, args []string) {
+	cache.Flush()
+	console.Success("Cache cleared")
+}
+
+func runCacheForget(cmd *cobra.Command, args []string) {
+	cache.Forget(cacheKey)
+	console.Success(fmt.Sprintf("Cache key [%s] deleted", cacheKey))
+}
+
+// forget 命令的选项
+var cacheKey string
+
+func init() {
+	// 注册 cache 命令的子命令
+	CmdCache.AddCommand(CmdCacheClear, CmdCacheForget)
+
+	// 注册 forget 命令的选项
+	CmdCacheForget.Flags().StringVarP(&cacheKey, "key", "k", "", "Cache key")
+	_ = CmdCacheForget.MarkFlagRequired("key")
+
+}

+ 1 - 1
app/http/controllers/api/v1/links_controller.go

@@ -12,6 +12,6 @@ type LinksController struct {
 }
 
 func (ctrl *LinksController) Index(c *gin.Context) {
-	links := link.All()
+	links := link.AllCached()
 	response.Data(c, links)
 }

+ 20 - 0
app/models/link/link_util.go

@@ -1,10 +1,14 @@
 package link
 
 import (
+	"time"
+
 	"github.com/gin-gonic/gin"
 
 	"github.com/runningwater/gohub/pkg/app"
+	"github.com/runningwater/gohub/pkg/cache"
 	"github.com/runningwater/gohub/pkg/database"
+	"github.com/runningwater/gohub/pkg/helpers"
 	"github.com/runningwater/gohub/pkg/paginator"
 )
 
@@ -23,6 +27,22 @@ func All() (links []Link) {
 	return
 }
 
+func AllCached() (links []Link) {
+	cacheKey := "links:aa"
+	expireTime := 120 * time.Minute
+	// 获取数据
+	cache.GetObject(cacheKey, &links)
+
+	if helpers.Empty(links) {
+		links = All()
+		if helpers.Empty(links) {
+			return links
+		}
+		cache.Set(cacheKey, links, expireTime)
+	}
+	return
+}
+
 func IsExist(field, value string) bool {
 	var count int64
 	database.DB.Model(Link{}).Where("? = ?", field, value).Count(&count)

+ 2 - 0
main.go

@@ -57,6 +57,8 @@ func main() {
 		// cmd.CmdTestCommand,
 
 		make.CmdMake,
+
+		cmd.CmdCache,
 	)
 
 	// 配置默认运行 Web 服务