Simplest Caching Explanation
19 August 2019
today I want to give one more explanation of how to use caching and slots for caching purposes. Also there are plenty of articles on the subject, I want to give one more with simplest recipe. So, if you need to cache something, you'll need to follow this procedure:
- declare you class as something that inherits IPrefetchable
- Create some placeholder in your class for storing items in the cache
- Implement Prefetch
- Implement GetSlot
Take a look on the code below, how it can be done:
public class ArTranFetcher : IPrefetchable { private List<ARTran> _arTranList = new List<ARTran>(); public void Prefetch() { _configurableList = new List<ARTran>(); var dbRecords = PXDatabase.Select<ARTran>(); foreach (var csAttributeDetailConfigurable in dbRecords) { _arTranList.Add(csAttributeDetailConfigurable); } } public static List<ARTran> GetARTrans() { var def = GetSlot(); return def._arTranList; } private static ArTranFetcher GetSlot() { return PXDatabase.GetSlot<ArTranFetcher>("ARtranFetcherSlot", typeof(ARTran)); } }
few more explanations on presented code.
- Placeholder will be _arTranList
- GetSlot will be monitoring database ARTran ( because of typeof(ARTran) statement ). So each time ARTRan will be updated, GetSlot will be executed automatically via framework.
- In your code you can get cached valus via following line of code: var arTrans = ArTranFetcher.GetARTrans();
with usage of such tricks, you can optimize plenty of staff. The only warning would be don't cache ARTran, as it will swallow up all memory of your server.