In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces in detail "how to use the Helper class in C#", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use the Helper class in C#" can help you solve your doubts.
Use background
Frequent access to the database by users in the project will lead to stutters and even blockages of the program. The use of cache can effectively reduce the frequency of users accessing the database and effectively reduce the pressure of concurrency. Protect the real server at the back end.
It is convenient for developers to call, so this article provides the helper class to encapsulate the cache. There are three Cache,SystemCache,RedisCache (default cache, system cache, Redis cache). Don't talk too much, let's do it!
Use method 1. Quote CSRedisCore
As you can see, csredis supports the .net40 / .net45 / .netstandard platform, which is relatively friendly.
two。 Add helper class code
CacheHelper.cs
/ public class CacheHelper helper class / public class CacheHelper {/ static constructor, initializing cache type / static CacheHelper () {cache = new SystemCache () If (true) / / project global variable class, you can define / / if (GlobalSwitch.OpenRedisCache) {try {RedisCache = new RedisCache (GlobalSwitch.RedisConfig) } catch {}} switch (GlobalSwitch.CacheType) {case CacheType.SystemCache:Cache = SystemCache;break; case CacheType.RedisCache:Cache = RedisCache;break; default:throw new Exception ("Please specify cache type!") ;} / default cache / public static ICache Cache {get;} / system cache / public static ICache SystemCache {get;} / Redis cache / public static ICache RedisCache {get }}
ICache.cs
/ public interface ICache operation interface class / public interface ICache {# cache setting cache / primary key / value void SetCache (string key, object value) / set cache / / Note: the default expiration type is absolute expiration / primary key / value / void SetCache (string key, object value, TimeSpan timeout) / set cache / / Note: the default expiration type is absolute expiration / primary key / / value / / expiration interval / expiration type void SetCache (string key, object value, TimeSpan timeout, ExpireType expireType) / set key expiration time / key value / interval from now on void SetKeyExpire (string key, TimeSpan expire) # endregion # region get cache / get cache / primary key object GetCache (string key); / get cache / primary key / data type T GetCache (string key) where T: class / whether there is a key value / primary key / bool ContainsKey (string key); # endregion # region delete cache / clear cache / primary key void RemoveCache (string key) # endregion} # region type definition / value information / public struct ValueInfoEntry {public string Value {get; set;} public string TypeName {get; set;} public TimeSpan? ExpireTime {get; set;} public ExpireType? ExpireType {get; set }} / Expiration type / public enum ExpireType {/ absolute expiration / Note: it has expired / Absolute since it was created for a period of time. / / relative expiration / / Note: the key expires after a period of time after it has not been accessed If this key is accessed all the time, the expiration time will be automatically extended / Relative,} # endregion
RedisCache.cs
/ Redis cache / public class RedisCache: ICache {/ constructor / / Note: please use / configuration string public RedisCache (string config) {_ redisCLient = new CSRedisClient (config);} private CSRedisClient _ redisCLient {get } public bool ContainsKey (string key) {return _ redisCLient.Exists (key);} public object GetCache (string key) {object value = null; var redisValue = _ redisCLient.Get (key); if (redisValue.IsNullOrEmpty ()) return null; ValueInfoEntry valueEntry = redisValue.ToString () .ToObject () If (valueEntry.TypeName = = typeof (string). AssemblyQualifiedName) value = valueEntry.Value; else value = valueEntry.Value.ToObject (Type.GetType (valueEntry.TypeName)); if (valueEntry.ExpireTime! = null & & valueEntry.ExpireType = = ExpireType.Relative) SetKeyExpire (key, valueEntry.ExpireTime.Value); return value } public T GetCache (string key) where T: class {return (T) GetCache (key);} public void SetKeyExpire (string key, TimeSpan expire) {_ redisCLient.Expire (key, expire);} public void RemoveCache (string key) {_ redisCLient.Del (key) } public void SetCache (string key, object value) {_ SetCache (key, value, null, null);} public void SetCache (string key, object value, TimeSpan timeout) {_ SetCache (key, value, timeout, ExpireType.Absolute) } public void SetCache (string key, object value, TimeSpan timeout, ExpireType expireType) {_ SetCache (key, value, timeout, expireType);} private void _ SetCache (string key, object value, TimeSpan? Timeout, ExpireType? ExpireType) {string jsonStr = string.Empty; if (value is string) jsonStr = value as string; else jsonStr = value.ToJson () ValueInfoEntry entry = new ValueInfoEntry {Value = jsonStr, TypeName = value.GetType (). AssemblyQualifiedName, ExpireTime = timeout, ExpireType = expireType}; string theValue = entry.ToJson (); if (timeout = = null) _ redisCLient.Set (key, theValue) Else _ redisCLient.Set (key, theValue, (int) timeout.Value.TotalSeconds);}
SystemCache.cs
/ system cache help class / public class SystemCache: ICache {public object GetCache (string key) {return HttpRuntime.Cache [key];} public T GetCache (string key) where T: class {return (T) HttpRuntime.Cache [key] } public bool ContainsKey (string key) {return GetCache (key)! = null;} public void RemoveCache (string key) {HttpRuntime.Cache.Remove (key);} public void SetKeyExpire (string key, TimeSpan expire) {object value = GetCache (key); SetCache (key, value, expire) } public void SetCache (string key, object value) {_ SetCache (key, value, null, null);} public void SetCache (string key, object value, TimeSpan timeout) {_ SetCache (key, value, timeout, ExpireType.Absolute) } public void SetCache (string key, object value, TimeSpan timeout, ExpireType expireType) {_ SetCache (key, value, timeout, expireType);} private void _ SetCache (string key, object value, TimeSpan? Timeout, ExpireType? ExpireType) {if (timeout = = null) HttpRuntime.Cache [key] = value; else {if (expireType = = ExpireType.Absolute) {DateTime endTime = DateTime.Now.AddTicks (timeout.Value.Ticks); HttpRuntime.Cache.Insert (key, value, null, endTime, Cache.NoSlidingExpiration) } else {HttpRuntime.Cache.Insert (key, value, null, Cache.NoAbsoluteExpiration, timeout.Value);}} 3. Use
4. Description
Redis is an open source (BSD licensed), in-memory data structure storage system that can be used as database, cache, and messaging middleware.
It is a non-relational database based on high-performance Key-Value and API in multiple languages. However, unlike traditional databases, the data of redis is stored in memory, so the storage and writing speed is very fast.
It supports many types of data structures, such as strings, hashes, lists, sets, sorted sets.
After reading this, the article "how to use the Helper class in C#" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about the article, please follow the industry information channel.
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.