Use anonymous functions to make caching more convenient

Source: Internet
Author: User
Tags anonymous

A data cache, generally by the data source, caching scheme, cache read to complete, now has an anonymous function, you can get the data source into anonymous functions. This allows you to declare a cache directly:

protected static CacheManager<Company> companyCache = new  CacheManager<Company>(new SmsDatabaseCache<Company>(), (key) =>
              {
                  return SmsDatabase.Instance.Context.Company.Where(c  => c.ShortNumber == (string)key).SingleOrDefault();
              });

Smsdatabasecache is a caching scheme, you can also use Simplecache,timeoutcache and so on to implement the Icache interface classes, because my icachemanager inherit from Icache, so you can also implement the level of even cache yo, For example:

Smsdatebasecache is a SmsDatabase.Instance.Context caching scheme that clears data operations when Linqcontextdispose.

Smsdatabasecache

    public class SmsDatabaseCache<TData> : ICache<TData>
     {
         private Dictionary<object, TData> cache = new  Dictionary<object, TData>();

         public SmsDatabaseCache()
         {
             SmsDatabase.Instance.PreDisposing += (s, e) =>
                 {
                     cache.Clear();
                 };
         }

         public TData Get(object key)
         {
             return cache[key];
         }

         public void Set(object key, TData data)
         {
             cache[key] = data;
         }

     }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.