Table of Contents

Class AutoIndexCache

Namespace
RentADeveloper.AutoIndexCache
Assembly
RentADeveloper.AutoIndexCache.dll

A thread-safe, lazy loading cache that automatically indexes cached items.

public class AutoIndexCache : IAutoIndexCache
Inheritance
AutoIndexCache
Implements
Inherited Members

Remarks

All public and protected members of AutoIndexCache are thread-safe and may be used concurrently from multiple threads.

Methods

Items<TItem>()

Gets the list of cached items of the type TItem from this cache.

public 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

TItem

The 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 TItem yet.

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.

public void SetItemsLoader<TItem>(Func<TItem[]> itemsLoader) where TItem : class

Parameters

itemsLoader Func<TItem[]>

The function that loads the cache items of the type TItem.

Type Parameters

TItem

The type of cache items itemsLoader loads.

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

itemsLoader is a null reference.