Cache

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
14 years ago
Hi,

I have problems with cache. When I use regex like its written it deas not remove anything from cache bacause regex.IsMatch always return false.

For example I send pattern "Nop.productoptionvalue.allbyproductidoptionid-{0}-{1}-{2}" and value of key is "Nop.productoptionvalue.allbyproductidoptionid-95-2-8". That gives me false.

I'm not good et regular expressions but isn't {0} for 0 symbols {1} for 1 symbol,...

Basicly only _cache.Clear() works because it does not compare anything.

For now I will chage this and use ... StartsWith and cut of averything from first "{" to the end for comparation.

Any other sudgestions?
14 years ago
Your understanding of the {n} characters is correct. This is a bug, but only for those cache keys with more than two parameters.

{n} means match the proceding element n times, so of course {0} (no match) and {1} (1 match) will work.

The best bet is to pass in just the start of the key e.g. "Nop.productoptionvalue.allbyproductidoption.id". Really there isn't any need to use RegularExpressions for checking against the cached items, you could just have:

string pattern = "Nop.productoptionvalue.allbyproductidoption.id";

string cacheditem = "Nop.productoptionvalue.allbyproductidoption.id-245";

if (cacheditem.StartsWith(pattern))
// remove item from cache

A quick fix would be to change the RemoveByPattern method to first remove any of the string.format parameters. A quick solution:

            int index = source.IndexOf("-");
            pattern = pattern.Remove(index, pattern.Length - index);

Hope this helps,

Ben
14 years ago
Thank for reply.

I already did this without regex but little bit different. I did not choose "-" but "{" as first char to look for. This will I assume be good for every pattern because all variables are at the end.

This is my code:

string paternCompare = pattern.Substring( 0, pattern.IndexOf( "{" ) > -1 ? pattern.IndexOf( "{" ) : pattern.Length );

while ( enumerator.MoveNext( ) )
{
    if ( enumerator.Key.ToString( ).StartsWith( paternCompare ) )
    {
        _cache.Remove( enumerator.Key.ToString( ) );
    }
}


I preaty much use more then 2 variables in patern because I implement multilanguage in shop so language is one more variable added to original one.
13 years ago
Hy bubbi!

Can you please explain a littlebit more, how, and where you implement these code because I also have a problem with caching languages.

Thanks in advance
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.