Skip to content

Conversation

@joshk0
Copy link

@joshk0 joshk0 commented Jul 13, 2020

In the original implementation, there is a risk of an infinitely
long-lived cache key that arises if HMSET for the cache entry succeeds,
but EXPIRE fails or is not executed for any reason.

By serializing the cache entry to JSON and storing that single string
in Redis, we can take advantage of SET's ability to atomically accept an
EX-piration time along with the set.

The downside is we now have to do a JSON.stringify for every cache store
we do, and JSON.parse for every cache hit / retrieval, but that is
better than cache entries that never expire.

Tests pass against a local redis after modifying 1 test case which stores a
value then stores the same key again without cache. In the HMSET
implementation, this test passed because HMSET does not modify the
expiration date of existing keys. However, calls to SET always remove
old values and any expiration.

Fixes #101.

In the original implementation, there is a risk of an infinitely
long-lived cache key that arises if HMSET for the cache entry succeeds,
but EXPIRE fails or is not executed for any reason.

By serializing the cache `entry` to JSON and storing that single string
in Redis, we can take advantage of SET's ability to atomically accept an
EX-piration time along with the set.

The downside is we now have to do a JSON.stringify for every cache store
we do, and JSON.parse for every cache hit / retrieval, but that is
better than cache entries that never expire.

Tests pass against a local redis after modifying 1 test case which stores a
value then stores the same key again without cache. In the HMSET
implementation, this test passed because HMSET does not modify the
expiration date of existing keys. However, calls to SET always remove
old values and any expiration. Therefore, use a different key to store
the explicitly non-expiring cache entry.

Fixes rv-kip#101.
@joshk0
Copy link
Author

joshk0 commented Jul 13, 2020

Output of redis-cli monitor during the changed unit test:

1594676437.435326 [0 127.0.0.1:40264] "set" "erct:test1" "{\"body\":\"test1 test1 test1\",\"type\":\"text/html\",\"status\":200,\"touched\":1594676437434,\"expire\":2}" "EX" "2"
1594676437.438695 [0 127.0.0.1:40264] "set" "erct:i don't expire" "{\"body\":\"test1 test1 test1\",\"type\":\"text/html\",\"status\":200,\"touched\":1594676437438,\"expire\":0}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cache never expire

1 participant