-
Notifications
You must be signed in to change notification settings - Fork 46
Expanded resources and caching #48
Description
When caching expanded resources the key being used is not consistent.
The cache key that is used for cacheing a resource is based on the resource href as well as its options. This is done in
stormpath-sdk-php/src/Stormpath/Cache/Cacheable.php
Lines 48 to 56 in bc64a5d
| private function createKey($href, $options) | |
| { | |
| $key = $href; | |
| if(!empty($options)) { | |
| $key .= ':' . implode(':',$options); | |
| } | |
| return $key; | |
| } |
The problem when a resource gets updated and saved the key being used to first delete it from the cache is just based on the href:
| $this->cache->delete($resource->getHref()); |
and not with its options.
As example, we always fetch accounts with expansion of its groups. The account data is saved to a cache key as:
https://api.stormpath.com/v1/accounts/7BVwoUmoMCfWdxrHOjtLm4:groups
Later we update and save changes to the account, the cache key will then be:
2. https://api.stormpath.com/v1/accounts/7BVwoUmoMCfWdxrHOjtLm4
which gives a miss as that key does not exist.
Second, the data is saved to the same key:
3. https://api.stormpath.com/v1/accounts/7BVwoUmoMCfWdxrHOjtLm4
which then holds the newly updated data.
Later we fetch the same account with expansion of its groups. The key for that will be:
4. https://api.stormpath.com/v1/accounts/7BVwoUmoMCfWdxrHOjtLm4:groups
which of course doesn't include the newly updated data, but the data from the 1. fetch.
I would suggest a solution to this to just skip the options our from the key creation. Which would keep the same cache key over the whole lifecycle of the cached item. Also it would work incremental as when ever new data is expanded it would be saved to the cached item (overwritten).