lock_and_cache icon indicating copy to clipboard operation
lock_and_cache copied to clipboard

cached? and clear methods not working properly?

Open mepatterson opened this issue 8 years ago • 4 comments
trafficstars

I cached a token from my API service:

hash = LockAndCache.lock_and_cache(:api_token, client: client_id) do
  get_token_from_remote
end

After this, it DOES appear to properly cache and not hit the remote service anymore.

but the cached? method doesn't seem to work...

LockAndCache.cached?(:api_token, client: client_id)
=> false

clear doesn't seem to work either

LockAndCache.clear(:api_token, client: client_id)
=> 0

Am I misunderstanding something about usage?

mepatterson avatar Aug 04 '17 16:08 mepatterson

Notably, something looks off here with debug mode:

when cache is hit using :api_token, client: client_id...

DEBUG -- : [lock_and_cache] A1 [:api_token] YWE0YTYzOTNiZjAzYzEyMWRkMmE0YTg4YTdmNWMwYjI4YjgwYTFjNw== 7f45b24684191170789c642e3d21c2429de8a0e1

when I try to clear it using :api_token, client: client_id...

DEBUG -- : [lock_and_cache] clear [:api_token, [[:client, "f2a13062e0c7b1ffcff1ecedb029da09f8490940b49f253154b02c2f0a1f4e4e"]]] YTZkNTA0YjY3MDM4ZGZlYWM5YTUyYzdhMWU3NjNjYzlmNmFiZTJlMg== 96b5b2167fef77da21d1052cdda6bd372f42451a

mepatterson avatar Aug 04 '17 17:08 mepatterson

hey @mepatterson i just now have time to work on lock_and_cache - sorry for the huge delay. did you figure this out?

seamusabshere avatar Sep 20 '18 11:09 seamusabshere

I think I just ended up working around it by always hand generating my own key string and not using the clear method. That said, one of the more annoying difficulties we've run into is the need to selectively clear things without doing LockAndCache.flush which flushes the ENTIRE redis cache (even non-lock-and-cache stuff). So the ability to do something like LockAndCache.clear_all or whatever, where it only clears stuff that LockAndCache has put into cache, would be awesome...

mepatterson avatar Sep 20 '18 14:09 mepatterson

hey @mepatterson

try putting the lock and cache storage in a different redis db "number"

e.g.

misc_redis = Redis.new db: 1 # non-lock-and-cache stuff
LockAndCache.lock_storage = Redis.new db: 2
LockAndCache.cache_storage = Redis.new db: 3

(updated: put the cache and lock storage in diff dbs themselves, for a total of 3 db numbers used)

seamusabshere avatar Sep 20 '18 16:09 seamusabshere