Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, after a while i've made extension methods for accessing my cache: </p> <pre><code>public static class MyExtensions { // temlplate for cache item key name private const string CacheKeyTemplate = "{0}_{1}"; private static string GetCachePrefixByType(Type type) { // this is just sample, implement it any way you want switch (type.Name) { case "Blog": return "b"; break; case "BlogPost": return "bp"; break; case "Comment": return "c"; break; default: throw new NotImplementedException(); } } // insert with key containing object type custom prefix and object id public static void Put(this Cache cache, object obj, long id) { cache.Insert(String.Format(CacheKeyTemplate, GetCachePrefixByType(obj.GetType()), id), obj); } // get by object type and id public static T Get&lt;T&gt;(this Cache cache, long id) { return (T)cache[String.Format(CacheKeyTemplate, GetCachePrefixByType(typeof(T)), id)]; } // get objects by WHERE expression public static List&lt;object&gt; Get(this Cache cache, Func&lt;object, bool&gt; f) { return cache.Cast&lt;DictionaryEntry&gt;().Select(e =&gt; e.Value).Where(f).ToList(); } // remove by object type and id public static void Remove&lt;T&gt;(this Cache cache, long id) { cache.Remove(String.Format(CacheKeyTemplate, GetCachePrefixByType(typeof(T)), id)); } // remove cache items by WHERE expression against stored objects public static void Remove(this Cache cache, Func&lt;object, bool&gt; f) { foreach (string key in cache.Cast&lt;DictionaryEntry&gt;().Where(de =&gt; f.Invoke(de.Value)).Select(de =&gt; de.Key)) { cache.Remove(key); } } } </code></pre> <p>and this is my classes to test:</p> <pre><code>private class Blog { public int BlogId { get; set; } public string Name { get; set; } } private class BlogPost { public int PostId { get; set; } public int BlogId { get; set; } public string Text { get; set; } } private class Comment { public int PostId { get; set; } public int CommentId { get; set; } public string Text { get; set; } } </code></pre> <p>and the test code itself:</p> <pre><code> // a blog Blog blog = new Blog{ BlogId = 1, Name = "Jim" }; // two blog posts BlogPost post1 = new BlogPost { PostId = 1, BlogId = 1, Text = "Aaaaaaaa" }; BlogPost post2 = new BlogPost { PostId = 2, BlogId = 1, Text = "Bbbbbbbbbb" }; // two comments to the 1st blog post Comment comment11 = new Comment { CommentId = 11, PostId = 1, Text = "qwerty" }; Comment comment12 = new Comment { CommentId = 12, PostId = 1, Text = "asdfg" }; // one comment to the 2nd blog post Comment comment21 = new Comment { CommentId = 21, PostId = 2, Text = "zxcvbn" }; // put it all to cache HttpRuntime.Cache.Put(blog, blog.BlogId); HttpRuntime.Cache.Put(post1, post1.PostId); HttpRuntime.Cache.Put(post2, post2.PostId); HttpRuntime.Cache.Put(comment11, comment11.CommentId); HttpRuntime.Cache.Put(comment12, comment12.CommentId); HttpRuntime.Cache.Put(comment21, comment21.CommentId); // get post #2 by its id BlogPost testPost = HttpRuntime.Cache.Get&lt;BlogPost&gt;(2); // testPost.Text = "Bbbbbbbbbb" // get all comments for post #1 IEnumerable&lt;Comment&gt; testComments = HttpRuntime.Cache.Get( x =&gt; (x is Comment) &amp;&amp; ((Comment)x).PostId == 1).Cast&lt;Comment&gt;(); // comments 11 and 12 are in the list // remove comment 21 HttpRuntime.Cache.Remove&lt;Comment&gt;(21); // test if it was removed comment21 = HttpRuntime.Cache.Get&lt;Comment&gt;(21); // null // remove anything having text property = "qwerty" HttpRuntime.Cache.Remove(x =&gt; x.GetType().GetProperty("Text") != null &amp;&amp; ((dynamic)x).Text == "qwerty"); // test if comment 11 it was removed comment11 = HttpRuntime.Cache.Get&lt;Comment&gt;(11); // null // but comment 12 should still exist comment12 = HttpRuntime.Cache.Get&lt;Comment&gt;(12); // it's there // remove anything from cache HttpRuntime.Cache.Remove(x =&gt; true); // cache items count should be zero int count = HttpRuntime.Cache.Count; // it is! </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload