Updating Data to Cache
Once data is added to the cache, you can update its contents against the existing key using the Insert
method. However, it must be ensured that the object is serialized just as with the Add
operation.
Updating Objects in Cache
In this example, a new object is added with an existing key.
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
product.UnitsInStock = 5; // updated units
string key = "Product:" + product.ProductID;
try{
//precondition: Cache is already initialized and item exists
cache.Insert(key, product);
}
catch (OperationFailedException ex){
// handle exception
}
Updating Objects Using CacheItem
In this example, a key is updated that has already been existing in cache with
object set as a property of CacheItem
.
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
product.UnitsInStock = 5; // updated units
CacheItem cacheItem = new CacheItem(product);
string key = "Product:" + product.ProductID;
try{
cache.Insert(key, cacheItem);
}
catch (OperationFailedException ex){
// handle exception
}