• Facebook
  • Twitter
  • Youtube
  • LinkedIn
  • RSS
  • Docs
  • Comparisons
  • Blogs
  • Try NCache
  • Contact Us
Try NCache
Show / Hide Table of Contents

Method Remove

Remove(String, LockHandle, CacheItemVersion, WriteThruOptions)

Removes the specified item from the ICache. You can also specify the write option, such that the item may be removed from both cache and data source. If version is specified, then item will only be removed if the specified version is still the most recent version in the cache.

Declaration
void Remove(string key, LockHandle lockHandle = null, CacheItemVersion version = null, WriteThruOptions writeThruOptions = null)
Parameters
Type Name Description
System.String key

Unique key of the item to be removed.

LockHandle lockHandle

If the item is locked, it can be removed only if the correct lockHandle is specified. lockHandle should be the same which was used initially to lock the item, otherwise you will get the 'OperationFailedException'.

CacheItemVersion version

The version of the item to be removed. The item is removed from the cache only if this is still the most recent version in the cache.

WriteThruOptions writeThruOptions

WriteThruOptions regarding updating the data source. This can be either WriteThru, WriteBehind or none.

Examples

The following example demonstrates how to remove a locked item in the cache with WriteThruOptions.

ICache cache = CacheManager.GetCache("demoCache");

Product product = new Product();
product.Id = 1;
product.Name = "Chai";

string key = "Product0";

CacheItemVersion version = cache.Add(key, product);

LockHandle lockHandle = new LockHandle();

object item = cache.Get<Product>(key, true, TimeSpan.Zero, ref lockHandle);

if (item != null)
{
    try
    {
        WriteThruOptions writeThruOptions = new WriteThruOptions(WriteMode.WriteThru, "ProdDataSource1");

        cache.Remove(key, lockHandle, version, writeThruOptions);
    }
    catch (OperationFailedException ex)
    {
    ...
    }
}

Remove<T>(String, out T, LockHandle, CacheItemVersion, WriteThruOptions)

Removes the specified item from the ICache and returns it to the application as an out parameter. You can also specify the write option such that the item may be removed from both cache and data source. If version is specified, then item will only be removed if the specified version is still the most recent version in the cache.

Declaration
bool Remove<T>(string key, out T removedItem, LockHandle lockHandle = null, CacheItemVersion version = null, WriteThruOptions writeThruOptions = null)
Parameters
Type Name Description
System.String key

Unique key of the item to be removed.

T removedItem

Out parameter through which the removed item from cache is returned

LockHandle lockHandle

If the item is locked, it can be removed only if the correct LockHandle is specified. LockHandle should be the same which was used initially to lock the item, otherwise you will get the 'OperationFailedException'.

CacheItemVersion version

The version of the item to be removed. The item is removed from the cache, only if this is still the most recent version in the cache.

WriteThruOptions writeThruOptions

WriteThruOptions regarding updating the data source. This can be either WriteThru, WriteBehind or none.

Returns
Type Description
System.Boolean

Flag which determines the status of remove operation. True if item was removed successfully from the cache or False if remove opeartaion failed.

Type Parameters
Name Description
T

Specifies the type of value obtained from the cache.

Examples

The following example demonstrates how you can remove an item from the cache.

ICache cache = CacheManager.GetCache("demoCache");

Product product = new Product();
product.Id = 1;
product.Name = "Chai";

string key = "Product0";

CacheItemVersion version = cache.Add(key, product);

LockHandle lockHandle = new LockHandle();
object item = cache.Get<Product>(key, true, TimeSpan.Zero, ref lockHandle);

WriteThruOptions writeThruOptions = new WriteThruOptions(WriteMode.WriteThru, "ProdDataSource1");

try
{
    Product removedProduct = null;
    if (cache.Remove<Product>(key, out removedProduct, lockHandle, version, writeThruOptions))
    {
        //Removed successfully
    }
    else
    {
        //Error Occured
    }
}
catch(Exception ex)
{
...
}
Exceptions
Type Condition
System.ArgumentNullException

Key contains a null reference.

System.ArgumentException

Key is not serializable.

Contact Us

PHONE

+1 214-619-2601   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Try NCache
Pricing
NCache Playground

Deployments
  • Cloud
  • Docker
  • Kubernetes
  • On-Premises
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • SignalR Backplane
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Blogs
  • Foundations
  • Videos
  • Comparisons
  • Shows
  • Whitepapers
  • Articles
  • Tech Talks
  • Magazine Articles
  • Third-Party Articles
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Docs
  • Schedule a Demo
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Terms of Use
  •  
  • Privacy Policy
  •  
  • Cookie Policy
© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.
  • Facebook
  • Twitter
  • Youtube
  • LinkedIn
  • RSS