/// <summary> /// overrides, supplies the default condition /// </summary> /// <param name="bag"></param> public override void Commit(ICommitBag cb) { lock (this._stateLock) { //commit first. if it kacks we don't do anything base.Commit(cb); //build and commit the conditions CommitBag conditionCommitBag = new CommitBag(); //foreach add, register a condition if (this.ExpirableFactory != null) { cb.ItemsToSave.WithEach(x => { //generate the eviction condition var evictionConditionLogic = this.ExpirableFactory.Perform(x) as LogicOfTo<IHasId, IExpirable>; //save the eviction condition in the eviction store, keyed by the storedobjectid of the item to save var conditionToSave = x.GetStoredObjectId().BuildAsId().HasContext(evictionConditionLogic.Result); //var soid = conditionToSave.GetStoredObjectId(); //var soid2 = StoredObjectId.New(typeof(ContextualIHasIdDecoration), x.GetStoredObjectId()); //bool isEq = soid.Equals(soid2); conditionCommitBag.MarkItemSaved(conditionToSave); }); } //foreach remove, remove a condition cb.ItemsToDelete.WithEach(x => { var delId = StoredObjectId.New(typeof(ContextualIHasIdDecoration), x); conditionCommitBag.MarkItemDeleted(delId); }); this.ExpirableStore.Commit(conditionCommitBag); } }
/// <summary> /// examines all eviction conditions and removes items that have an eviction condition of true. /// If an item's eviction condition is mutable, it will be mutated (eg. touched) on every get /// </summary> public void Evict() { List<ContextualIHasIdDecoration> itemsToEvict = new List<ContextualIHasIdDecoration>(); //search the eviction store for evicts var evictions = this.ExpirableStore.GetAll(); foreach (var each in evictions) { var eachItem = each as ContextualIHasIdDecoration; if (eachItem != null && eachItem.Context != null) { IExpirable exp = eachItem.Context as IExpirable; if (exp.IsExpired()) { itemsToEvict.Add(eachItem); } } } //build deletes and commit them var mainCommitBag = new CommitBag(); var expCommitBag = new CommitBag(); itemsToEvict.WithEach(x => { StoredObjectId soid = x.Id as StoredObjectId; mainCommitBag.MarkItemDeleted(soid); expCommitBag.MarkItemDeleted(x.GetStoredObjectId()); }); //remove the item specified by the expirable policy. this.Commit(mainCommitBag); //now delete from the expiry store this.ExpirableStore.Commit(expCommitBag); //raise events (outside of state lock) itemsToEvict.WithEach(x => { this.OnItemEvicted(x, x.Context as IExpirable); }); }
/// <summary> /// registers an item with a specific eviction condition. If the condition is mutable, every touch/get of the item will result /// in a condition mutation. /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="evictionCondition">may be null</param> public virtual void Commit(ICommitBag cb, IExpirable evictionCondition) { lock (this._stateLock) { //commit first. if it kacks we don't do anything this.Decorated.Commit(cb); //build and commit the conditions CommitBag conditionCommitBag = new CommitBag(); //foreach add, register a condition cb.ItemsToSave.WithEach(x => { //save the eviction condition in the eviction store, keyed by the storedobjectid of the item to save var conditionToSave = x.GetStoredObjectId().BuildAsId().HasContext(evictionCondition); conditionCommitBag.MarkItemSaved(conditionToSave); }); //foreach remove, remove a condition cb.ItemsToDelete.WithEach(x => { var delId = StoredObjectId.New(typeof(ContextualIHasIdDecoration), x); conditionCommitBag.MarkItemDeleted(delId); }); this.ExpirableStore.Commit(conditionCommitBag); } }