cache.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package cmd
  2. import (
  3. "fmt"
  4. "github.com/spf13/cobra"
  5. "github.com/runningwater/gohub/pkg/cache"
  6. "github.com/runningwater/gohub/pkg/console"
  7. )
  8. var CmdCache = &cobra.Command{
  9. Use: "cache",
  10. Short: "Cache Management",
  11. }
  12. var CmdCacheClear = &cobra.Command{
  13. Use: "clear",
  14. Short: "Clear Cache",
  15. Run: runCacheClear,
  16. }
  17. var CmdCacheForget = &cobra.Command{
  18. Use: "forget",
  19. Short: "Delete redis key, example: gohub cache forget key",
  20. Run: runCacheForget,
  21. }
  22. func runCacheClear(cmd *cobra.Command, args []string) {
  23. cache.Flush()
  24. console.Success("Cache cleared")
  25. }
  26. func runCacheForget(cmd *cobra.Command, args []string) {
  27. cache.Forget(cacheKey)
  28. console.Success(fmt.Sprintf("Cache key [%s] deleted", cacheKey))
  29. }
  30. // forget 命令的选项
  31. var cacheKey string
  32. func init() {
  33. // 注册 cache 命令的子命令
  34. CmdCache.AddCommand(CmdCacheClear, CmdCacheForget)
  35. // 注册 forget 命令的选项
  36. CmdCacheForget.Flags().StringVarP(&cacheKey, "key", "k", "", "Cache key")
  37. _ = CmdCacheForget.MarkFlagRequired("key")
  38. }