In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to build a custom file cache in ASP.NET". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
ASP.NET 's output cache, or static HTML, was memory-based until .NET 4.0. This means that if our site contains a lot of cache, it is easy to consume native memory. Now, with the help of OutputCacheProvider in .NET 4.0, we have a variety of options to create our own cache. For example, we can store the HTML output cache in a memcached distributed cluster server, or in MongoDB. Of course, we can also store the cache as a file on the hard disk, which is the cheapest way to consider scalability.
1:OutputCacheProvider
OutputCacheProvider is an abstract base class, and we need four methods of override. They are:
The Add method inserts the specified item into the output cache.
The Get method, which returns a reference to the specified item in the output cache.
The Remove method that removes the specified item from the output cache.
The Set method inserts the specified item into the output cache or overwrites the item if it is already cached.
2: create your own file cache processing class
The type is FileCacheProvider and the code is as follows:
Public class FileCacheProvider: OutputCacheProvider {private static readonly ILog log = LogManager.GetLogger (System.Reflection.MethodBase.GetCurrentMethod () .DeclaringType); public override void Initialize (string name, NameValueCollection attributes) {base.Initialize (name, attributes); CachePath = HttpContext.Current.Server.MapPath (attributes ["cachePath"]);} public override object Add (string key, object entry, DateTime utcExpiry) {Object obj = Get (key); if (obj! = null) / / this step is important {return obj } Set (key,entry,utcExpiry); return entry;} public override object Get (string key) {string path = ConvertKeyToPath (key); if (! File.Exists (path)) {return null;} CacheItem item = null; using (FileStream file = File.OpenRead (path)) {var formatter = new BinaryFormatter (); item = (CacheItem) formatter.Deserialize (file);} if (item.ExpiryDate)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.