/// <summary> /// Adds new item to persistence store /// </summary> /// <param name="storageKey">Unique key for storage item.</param> /// <param name="newItem">Item to be added to cache. May not be null.</param> protected override void AddNewItem(int storageKey, CacheItem newItem) { string key = newItem.Key; byte[] valueBytes = SerializationUtility.ToBytes(newItem.Value); if (encryptionProvider != null) { valueBytes = encryptionProvider.Encrypt(valueBytes); } byte[] expirationBytes = SerializationUtility.ToBytes(newItem.GetExpirations()); byte[] refreshActionBytes = SerializationUtility.ToBytes(newItem.RefreshAction); CacheItemPriority scavengingPriority = newItem.ScavengingPriority; DateTime lastAccessedTime = newItem.LastAccessedTime; DbCommand insertCommand = database.GetStoredProcCommand("AddItem"); database.AddInParameter(insertCommand, "@partitionName", DbType.String, partitionName); database.AddInParameter(insertCommand, "@storageKey", DbType.Int32, storageKey); database.AddInParameter(insertCommand, "@key", DbType.String, key); database.AddInParameter(insertCommand, "@value", DbType.Binary, valueBytes); database.AddInParameter(insertCommand, "@expirations", DbType.Binary, expirationBytes); database.AddInParameter(insertCommand, "@refreshAction", DbType.Binary, refreshActionBytes); database.AddInParameter(insertCommand, "@scavengingPriority", DbType.Int32, scavengingPriority); database.AddInParameter(insertCommand, "@lastAccessedTime", DbType.DateTime, lastAccessedTime); database.ExecuteNonQuery(insertCommand); }
/// <summary> /// Adds new item to persistence store /// </summary> /// <param name="storageKey">Unique key for storage item.</param> /// <param name="newItem">Item to be added to cache. May not be null.</param> protected override void AddNewItem(int storageKey, CacheItem newItem) { string key = newItem.Key; byte[] valueBytes = SerializationUtility.ToBytes(newItem.Value); if (this.encryptionProvider != null) { valueBytes = this.encryptionProvider.Encrypt(valueBytes); } byte[] expirationBytes = SerializationUtility.ToBytes(newItem.GetExpirations()); byte[] refreshActionBytes = SerializationUtility.ToBytes(newItem.RefreshAction); CacheItemPriority scavengingPriority = newItem.ScavengingPriority; DateTime lastAccessedTime = newItem.LastAccessedTime; DbCommand insertCommand = database.GetStoredProcCommand("AddItem"); database.AddInParameter(insertCommand, "@partitionName", DbType.String, partitionName); database.AddInParameter(insertCommand, "@storageKey", DbType.Int32, storageKey); database.AddInParameter(insertCommand, "@key", DbType.String, key); database.AddInParameter(insertCommand, "@value", DbType.Binary, valueBytes); database.AddInParameter(insertCommand, "@expirations", DbType.Binary, expirationBytes); database.AddInParameter(insertCommand, "@refreshAction", DbType.Binary, refreshActionBytes); database.AddInParameter(insertCommand, "@scavengingPriority", DbType.Int32, scavengingPriority); database.AddInParameter(insertCommand, "@lastAccessedTime", DbType.DateTime, lastAccessedTime); database.ExecuteNonQuery(insertCommand); }
/// <summary> /// Stores specified CacheItem into IsolatedStorage at location specified in constructor /// </summary> /// <param name="itemToStore">The <see cref="CacheItem"/> to store.</param> public void Store(CacheItem itemToStore) { keyField.Write(itemToStore.Key, false); valueField.Write(itemToStore.Value, true); scavengingPriorityField.Write(itemToStore.ScavengingPriority, false); refreshActionField.Write(itemToStore.RefreshAction, false); expirationsField.Write(itemToStore.GetExpirations(), false); lastAccessedField.Write(itemToStore.LastAccessedTime, false); }
/// <summary> /// Stores specified CacheItem into IsolatedStorage at location specified in constructor /// </summary> /// <param name="itemToStore">The <see cref="CacheItem"/> to store.</param> public void Store(CacheItem itemToStore) { keyField.Write(itemToStore.Key, false); valueField.Write(itemToStore.Value, true); scavengingPriorityField.Write(itemToStore.ScavengingPriority, false); refreshActionField.Write(itemToStore.RefreshAction, false); expirationsField.Write(itemToStore.GetExpirations(), false); lastAccessedField.Write(itemToStore.LastAccessedTime, false); }
public void ReadCacheItemWithOneExpirationAction() { CacheItem itemToStore = new CacheItem("monkey", "baboon", CacheItemPriority.NotRemovable, null, new NeverExpired()); CacheItem readItem = DoCacheItemRoundTripToStorage(itemToStore, false); ICacheItemExpiration[] expirations = readItem.GetExpirations(); Assert.AreEqual(1, expirations.Length); Assert.AreEqual(typeof(NeverExpired), expirations[0].GetType()); }
public void CtorForLoadingInitializesExpirationsToMatchLastAccessedTime() { DateTime historicalTimestamp = DateTime.Now - TimeSpan.FromHours(1.0); CacheItem item = new CacheItem(historicalTimestamp, "foo", "bar", CacheItemPriority.NotRemovable, null, new SlidingTime(TimeSpan.FromHours(17.0), DateTime.Now)); ICacheItemExpiration[] expirations = item.GetExpirations(); SlidingTime expiration = expirations[0] as SlidingTime; Assert.AreEqual(historicalTimestamp, expiration.TimeLastUsed); }
public void CtorForLoadingInitializesExpirationsToMatchLastAccessedTime() { DateTime historicalTimestamp = DateTime.Now - TimeSpan.FromHours(1.0); CacheItem item = new CacheItem(historicalTimestamp, "foo", "bar", CacheItemPriority.NotRemovable, null, new SlidingTime(TimeSpan.FromHours(17.0), DateTime.Now)); ICacheItemExpiration[] expirations = item.GetExpirations(); SlidingTime expiration = expirations[0] as SlidingTime; Assert.AreEqual(historicalTimestamp, expiration.TimeLastUsed); }
public void ReadCacheItemWithThreeExpirationActions() { CacheItem itemToStore = new CacheItem("monkey", "baboon", CacheItemPriority.NotRemovable, null, new NeverExpired(), new AlwaysExpired(), new AbsoluteTime(TimeSpan.FromSeconds(1.0))); CacheItem readItem = DoCacheItemRoundTripToStorage(itemToStore, false); ICacheItemExpiration[] expirations = readItem.GetExpirations(); Assert.AreEqual(3, expirations.Length); Assert.AreEqual(typeof(NeverExpired), expirations[0].GetType()); Assert.AreEqual(typeof(AlwaysExpired), expirations[1].GetType()); Assert.AreEqual(typeof(AbsoluteTime), expirations[2].GetType()); }
public void DataCanBeRetrievedWithNoEncryptionProvider() { unencryptedBackingStore.Add(new CacheItem("key", "value", CacheItemPriority.Normal, new MockRefreshAction(), new AlwaysExpired())); Hashtable dataInCache = unencryptedBackingStore.Load(); CacheItem retrievedItem = (CacheItem)dataInCache["key"]; Assert.AreEqual("key", retrievedItem.Key); Assert.AreEqual("value", retrievedItem.Value); Assert.AreEqual(CacheItemPriority.Normal, retrievedItem.ScavengingPriority); Assert.AreEqual(typeof(MockRefreshAction), retrievedItem.RefreshAction.GetType()); Assert.AreEqual(typeof(AlwaysExpired), retrievedItem.GetExpirations()[0].GetType()); }
/// <summary> /// Stores specified CacheItem into IsolatedStorage at location specified in constructor /// </summary> /// <param name="itemToStore">The <see cref="CacheItem"/> to store.</param> public void Store(CacheItem itemToStore) { if (itemToStore == null) { throw new ArgumentNullException("itemToStore"); } keyField.Write(itemToStore.Key, false); valueField.Write(itemToStore.Value, true); scavengingPriorityField.Write(itemToStore.ScavengingPriority, false); refreshActionField.Write(itemToStore.RefreshAction, false); expirationsField.Write(itemToStore.GetExpirations(), false); lastAccessedField.Write(itemToStore.LastAccessedTime, false); }
public void CanLoadOneItemFromTable() { RefreshAction refreshAction = new RefreshAction(); CacheItem addedItem = new CacheItem("key", "value", CacheItemPriority.NotRemovable, refreshAction, new SlidingTime(TimeSpan.FromHours(1)), new AbsoluteTime(TimeSpan.FromHours(1.0))); backingStore.Add(addedItem); Hashtable retrievedData = backingStore.Load(); Assert.AreEqual(1, retrievedData.Count); CacheItem item = (CacheItem)retrievedData["key"]; Assert.AreEqual("key", item.Key); Assert.AreEqual("value", item.Value); Assert.AreEqual(CacheItemPriority.NotRemovable, item.ScavengingPriority); Assert.AreEqual(typeof(RefreshAction), item.RefreshAction.GetType()); Assert.IsFalse(Object.ReferenceEquals(refreshAction, item.RefreshAction)); Assert.AreEqual(2, item.GetExpirations().Length); Assert.AreEqual(typeof(SlidingTime), item.GetExpirations()[0].GetType()); Assert.AreEqual(typeof(AbsoluteTime), item.GetExpirations()[1].GetType()); }
void ReadMinimalCacheItemFromIsolatedStorageEncrypted(bool encrypted) { DateTime historicalTimestamp = DateTime.Now - TimeSpan.FromHours(1.0); CacheItem itemToStore = new CacheItem("key1", "value1", CacheItemPriority.Normal, null); itemToStore.SetLastAccessedTime(historicalTimestamp); CacheItem readItem = DoCacheItemRoundTripToStorage(itemToStore, encrypted); Assert.AreEqual("key1", readItem.Key); Assert.AreEqual("value1", readItem.Value); Assert.AreEqual(CacheItemPriority.Normal, readItem.ScavengingPriority); Assert.IsNull(readItem.RefreshAction); ICacheItemExpiration[] expirations = readItem.GetExpirations(); Assert.AreEqual(0, expirations.Length); Assert.AreEqual(historicalTimestamp, readItem.LastAccessedTime); }
public void DecryptedDataCanBeReadBackFromDatabase() { IStorageEncryptionProvider encryptionProvider = null; encryptionProvider = EnterpriseLibraryFactory.BuildUp <IStorageEncryptionProvider>("Fred"); DataBackingStore encryptingBackingStore = new DataBackingStore(db, "encryptionTests", encryptionProvider); encryptingBackingStore.Add(new CacheItem("key", "value", CacheItemPriority.Normal, new MockRefreshAction(), new AlwaysExpired())); Hashtable dataInCache = encryptingBackingStore.Load(); CacheItem retrievedItem = (CacheItem)dataInCache["key"]; Assert.AreEqual("key", retrievedItem.Key); Assert.AreEqual("value", retrievedItem.Value); Assert.AreEqual(CacheItemPriority.Normal, retrievedItem.ScavengingPriority); Assert.AreEqual(typeof(MockRefreshAction), retrievedItem.RefreshAction.GetType()); Assert.AreEqual(typeof(AlwaysExpired), retrievedItem.GetExpirations()[0].GetType()); }