开发者

.NET 4 Caching Support

开发者 https://www.devze.com 2023-03-05 04:11 出处:网络
I understa开发者_开发技巧nd the .NET 4 Framework has caching support built into it. Does anyone have any experience with this, or could provide good resources to learn more about this?

I understa开发者_开发技巧nd the .NET 4 Framework has caching support built into it. Does anyone have any experience with this, or could provide good resources to learn more about this?

I am referring to the caching of objects (entities primarily) in memory, and probably the use of System.Runtime.Caching.


I assume you are getting at this, System.Runtime.Caching, similar to the System.Web.Caching and in a more general namespace.

See http://deanhume.com/Home/BlogPost/object-caching----net-4/37

and on the stack,

is-there-some-sort-of-cachedependency-in-system-runtime-caching and,

performance-of-system-runtime-caching.

Could be useful.


I've not made use of it myself, but if you're just caching simple objects in memory, you're probably referring to the MemoryCache class, in the System.Runtime.Caching namespace. There is a little example of how to use it at the end of the page.

Edit: To make it look like I've actually done some work for this answer, here's the sample from that page! :)

private void btnGet_Click(object sender, EventArgs e)
{
    ObjectCache cache = MemoryCache.Default;
    string fileContents = cache["filecontents"] as string;

    if (fileContents == null)
    {
        CacheItemPolicy policy = new CacheItemPolicy();

        List<string> filePaths = new List<string>();
        filePaths.Add("c:\\cache\\example.txt");

        policy.ChangeMonitors.Add(new 
        HostFileChangeMonitor(filePaths));

        // Fetch the file contents.
        fileContents = 
            File.ReadAllText("c:\\cache\\example.txt");

        cache.Set("filecontents", fileContents, policy);
    }

    Label1.Text = fileContents;
}

It's interesting because it shows that you can apply dependencies to the cache, much like in the classic ASP.NET cache. The big difference here is that you don't have a dependency on the System.Web assembly.


MemoryCache in the framework is a good place to start, but you might also like to consider LazyCache because it has a simpler API than memory cache and has built in locking as well as some other nice features. It is available on nuget: PM> Install-Package LazyCache

// Create our cache service using the defaults (Dependency injection ready).
// Uses MemoryCache.Default as default so cache is shared between instances
IAppCache cache = new CachingService();

// Declare (but don't execute) a func/delegate whose result we want to cache
Func<ComplexObjects> complexObjectFactory = () => methodThatTakesTimeOrResources();

// Get our ComplexObjects from the cache, or build them in the factory func 
// and cache the results for next time under the given key
ComplexObject cachedResults = cache.GetOrAdd("uniqueKey", complexObjectFactory);

I recently wrote this article about getting started with caching in dot net that you may find useful.

(Disclaimer: I am the author of LazyCache)


Hope you are referring to System.Runtime.Caching of .Netframework 4.0

Below link is the good starting point: Here


MSDN article "ASP.NET Caching: Techniques and Best Practices" is a great start.

0

精彩评论

暂无评论...
验证码 换一张
取 消