Interface IAutoIndexCache
- Namespace
- RentADeveloper.AutoIndexCache
- Assembly
- RentADeveloper.AutoIndexCache.dll
Represents a thread-safe, lazy loading cache that automatically indexes cached items.
public interface IAutoIndexCache
Methods
Items<TItem>()
Gets the list of cached items of the type TItem from this cache.
IItemsList<TItem> Items<TItem>() where TItem : class
Returns
- IItemsList<TItem>
An instance of IItemsList<TItem> that allows to access the cached items of the type
TItem.
Type Parameters
TItemThe type of cache items to get.
Examples
var cache = new AutoIndexCache();
cache.SetItemsLoader<User>(this.LoadUsers);
var users = cache.Items<User>().GetAllItems();
Exceptions
- MissingItemsLoaderException
No cache items loader has been set for the cache item type
TItemyet.
SetItemsLoader<TItem>(Func<TItem[]>)
Sets the function that loads the cache items of the type TItem when cache items of that type are requested from the cache.
If a cache items loader has already been set for the type TItem on this instance,
the old cache items loader is replaced and the corresponding ItemsList<TItem> is reset,
so the new cache items loader will be used to load the cache items the next time the cache items are requested from the cache.
void SetItemsLoader<TItem>(Func<TItem[]> itemsLoader) where TItem : class
Parameters
itemsLoaderFunc<TItem[]>The function that loads the cache items of the type
TItem.
Type Parameters
TItemThe type of cache items
itemsLoaderloads.
Examples
var cache = new AutoIndexCache();
cache.SetItemsLoader<User>(() => this.LoadUsers());
var users = cache.Items<User>().GetAllItems();
Remarks
The specified cache items loader function may not return null.
The specified cache items loader function may not access the cached items of the type TItem inside its body.
For example, the cache items loader for the cache item type "User" may not call the following methods inside its body:
- IItemsList<User>.GetAllItems
- IItemsList<User>.Reset
- IItemsList<User>.NonUniqueIndex<TKey>
- IItemsList<User>.UniqueIndex<TKey>
However, the cache items loader is allowed to access cache items of other types. For example, the cache items loader for the cache item type "User" is allowed call the following methods inside its body:
- IItemsList<Group>.GetAllItems
- IItemsList<Group>.Reset
- IItemsList<Group>.NonUniqueIndex<TKey>
- IItemsList<Group>.UniqueIndex<TKey>
However, cyclic dependencies are not allowed. For example, when the cache items loader of the cache item type "User" accesses cache items of the type "Group" and the cache items loader of the type "Group" accesses cache items of the type "User" (meaning User > Group > User), this is not allowed.
Exceptions
- ArgumentNullException
itemsLoaderis a null reference.