hedis-pile-0.5.3: Caching mandatory data with Redis

Safe HaskellSafe-Infered



Solution for caching mandatory data with Redis.

In many cases, requires not just pick up or put the data into the cache. As a rule, data are required.

... check the cache ... if the value is missing, run the calculations ... put value to cache ... Tedious

Solution is quite simple - collapse all of these steps in one operation.





:: forall ma d . (MonadIO ma, ma ~ Redis, Binary d) 
=> ByteString

Prefix for key and tags.

-> ByteString

Key in cache. Key will be stored as prefix:key

-> Maybe ByteString

Optional expect value. If it matches the value in the cache, pile will return Nothing. This is very useful when data in cache can be described with hash. For example, webpage ETag.

-> (forall mb. MonadIO mb => mb (d, ByteString, [ByteString], Maybe Integer))

Computation that returns data, expect value, tags and optional TTL. All tags will be stored as prefix:tag.

-> ma (Maybe d) 

Stores computation results in Redis. Computation fires only if data absent in cache. Of course, to refresh the data, they must first remove it from the cache.

Computation controls everything except prefix and key.

In background data is stored in Redis as HashSet with two fields: d for serialized data and e for expect field.

Time complexity depends on the situation.

  • O(2) data exists in cache, expect matches.
  • O(2) data exists in cache, expect value is Nothing.
  • O(3) data exists in cache, but expect value not matches value in cache.
  • In all other cases time complexity does not make sense