memcache.go 683 B

123456789101112131415161718192021222324252627282930313233
  1. package cache
  2. import (
  3. "context"
  4. "github.com/patrickmn/go-cache"
  5. "time"
  6. )
  7. type MemCache struct {
  8. engine *cache.Cache
  9. }
  10. func (cache *MemCache) Set(ctx context.Context, key string, value any) {
  11. cache.engine.Set(key, value, 0)
  12. }
  13. func (cache *MemCache) SetEx(ctx context.Context, key string, value any, expire time.Duration) {
  14. cache.engine.Set(key, value, expire)
  15. }
  16. func (cache *MemCache) Get(ctx context.Context, key string) (value any, ok bool) {
  17. return cache.engine.Get(key)
  18. }
  19. func (cache *MemCache) Del(ctx context.Context, key string) {
  20. cache.engine.Delete(key)
  21. }
  22. func NewMemCache() *MemCache {
  23. return &MemCache{
  24. engine: cache.New(time.Hour, time.Minute*90),
  25. }
  26. }