-
Notifications
You must be signed in to change notification settings - Fork 217
How to remove groups of entries #48
Comments
That's not supported in the current design. |
Btw how to solved if i want to delete all the keys that start with "SOMEKEY.*"? In the current .net 4 memoryobject usually i get allthekey then check the regex then remove the key. |
I recommend using shared cache triggers or cache entry links to remove groups of items. Triggers: Create a trigger and assign it to each item in the group. Fire the trigger when you want to remove all the items. Entry links: Create one item that is the root item. Use EntryLink's to make all other items depend on that one. When you want to remove all the items just remove the root item and the rest will be removed automatically. |
most of the time people store the key is in this format. Keys: When we want to perform CRUD operation CREATE, UPDATE, DELETE Q1. Can you explain how that the pattern using trigger & entry links for above case. |
A2: The cache is considered highly unstable where entries could expire or be removed at any time. This would make simple code like the following unreliable.
A1: Bulk remove with shared expiration triggers:
Bulk remove with EntryLinks:
|
Remove on Trigger - Is work fine to delete all keys. I got error on EntryLinks
From k test result |
@Praburaj Can you run this and see what I did wrong? |
@Tratcher I'll give it a try. |
Ok, now I remember how entry links work. They don't create a direct link between keys, they instead copy any existing expiration information (triggers or timers) from the parent key to any sub keys. // Remove on trigger
var cts = new CancellationTokenSource();
result = cache.GetOrSet("Parent Key", state, context =>
{
context.AddExpirationTrigger(new CancellationTokenTrigger(cts.Token));
return "Parent Value";
});
result = cache.Set("Dependent Key", context =>
{
// Share the CancellationTokenTrigger via EntryLink
var link = new EntryLink();
var parentValue = cache.Get("Parent Key", link);
context.AddEntryLink(link);
return "Dependent Value" + parentValue;
});
// ...
Console.WriteLine(cache.Get("Dependent Key"));
cts.Cancel(); // Also removes linked "Dependent Key"
Console.WriteLine(cache.Get("Dependent Key")); |
@Tratcher are you able to change the example test that i write? Appreciate your help |
var cache = new MemoryCache(new MemoryCacheOptions()
{
ListenForMemoryPressure = false
});
var langCts = new CancellationTokenSource();
cache.Set("membersite.language.", ctx =>
{
ctx.AddExpirationTrigger(new CancellationTokenTrigger(langCts.Token));
return "Language Pattern";
});
var link = new EntryLink();
// Get all of the expiration triggers from the parent key
var parentValue = cache.Get("membersite.language.", link);
Assert.Equal("Language Pattern", parentValue);
var result = cache.Set("membersite.language.id-1", context =>
{
// Copy the expiration triggers to this entry
context.AddEntryLink(link);
return "en-US";
});
var curCts = new CancellationTokenSource();
cache.Set("membersite.currency.", ctx =>
{
ctx.AddExpirationTrigger(new CancellationTokenTrigger(curCts.Token));
return "Currency Pattern";
});
link = new EntryLink();
parentValue = cache.Get("membersite.currency.", link);
Assert.Equal("Currency Pattern", parentValue);
result = cache.Set("membersite.currency.id-1", context => {
context.AddEntryLink(link);
return "RMB";
});
var currency = cache.Get<string>("membersite.currency.id-1");
Assert.Equal("RMB", currency);
var language = cache.Get<string>("membersite.language.id-1");
Assert.Equal("en-US", language);
// Remove just the root key
cache.Remove("membersite.language.");
parentValue = cache.Get<string>("membersite.language.");
Assert.Null(parentValue);
language = cache.Get<string>("membersite.language.id-1");
Assert.Equal("en-US", language);
// Remove all the associated lang keys.
langCts.Cancel();
language = cache.Get<string>("membersite.language.id-1");
Console.WriteLine("Language: " + language);
Assert.Null(language);
currency = cache.Get<string>("membersite.currency.id-1");
Assert.Equal("RMB", currency); |
I look into the test project, How can I get all the keys?
The text was updated successfully, but these errors were encountered: