Caching

Caching

Is hard to do right.

Strategies

Save keys in a heirarchy

i.e.

Product
Product.ABC11123

Clear [Product.ABC11123] removes one product
Clear [Product] removes all keys pattern matching/starting with Product

Easy to understand but can be dangerous and somewhat difficult to track usage. Some of this can be eased with code constructs such as enumerations used as keys or parts of keys rather than just plain strings.

Incorporate dates in keys

Need something to expire at a certain infrequent point but don't want to reduce the caching timeout just to enable an edge scenario?

Adding a DateTime to the key can ensure that when the time period is over the key becomes invalid and the data is automatically refreshed

Product.ABC11123 ("{0}.{1}", myEnum.Product, product.Sku)
Product.ABC11123.22/12 ( "{0}.{1}.{2:dd/mm}" myEnum.Product, product.Sku, DateTime.Now  )
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License