func (c *Cache) exists(key string) error { val, _ := c.client.Do("EXISTS", key) if val.(int64) == 1 { return nil } return errors.NewNonExistingKey(key) }
// CompareAndReplace validates the token with the token in the store. If the // tokens match, we will replace the value and return true. If it doesn't, we // will not replace the value and return false. func (c *Cache) CompareAndReplace(token, key string, value []byte, ttl int64) error { if err := c.exists(key); err != nil { return err } if c.items[key].token != token { return errors.NewNonExistingKey(key) } return c.Set(key, value, ttl) }
// exists checks if a key is stored in the cache. // // If the key is stored in the cache, but the expiry date has passed, we will // remove the item from the cache and return false. If the expiry has not passed // yet, it will return false. func (c *Cache) exists(key string) error { cachedItem, exists := c.items[key] if exists { if !cachedItem.expire || time.Now().Before(cachedItem.expiry) { c.lru(key) return nil } // Item is expired, delete it and act as it doesn't exist c.Delete(key) } return errors.NewNonExistingKey(key) }