ETCD后台服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
834 B

3 years ago
  1. package cache
  2. import (
  3. "time"
  4. "github.com/patrickmn/go-cache"
  5. )
  6. /* 默认缓存 - 内存 */
  7. var (
  8. // DefaultMemCache 默认缓存对象
  9. DefaultMemCache Cache
  10. )
  11. func init() {
  12. cli := cache.New(7*24*time.Hour, 10*time.Second)
  13. DefaultMemCache = &MemCache{
  14. cli: cli,
  15. }
  16. }
  17. // MemCache 内存缓存 https://github.com/patrickmn/go-cache
  18. type MemCache struct {
  19. cli *cache.Cache
  20. }
  21. // Get 获取一个缓存
  22. func (mem *MemCache) Get(key string) (val string, exist bool) {
  23. valI, exist := mem.cli.Get(key)
  24. if exist == true {
  25. val = valI.(string)
  26. }
  27. return
  28. }
  29. // Set 设置一个值
  30. func (mem *MemCache) Set(key, val string, expiration time.Duration) {
  31. mem.cli.Set(key, val, expiration)
  32. }
  33. // Del 删除知道keys
  34. func (mem *MemCache) Del(key ...string) (err error) {
  35. for _, k := range key {
  36. mem.cli.Delete(k)
  37. }
  38. return
  39. }