In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "simple use of redis database", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "the simple use of redis database"!
Using Newtonsoft.Json
Using StackExchange.Redis
Using System
Using System.Collections.Generic
Using System.Configuration
Using System.Linq
Using System.Text
Using System.Threading.Tasks
Namespace ConsoleApp
{
/ / /
/ Redis operation class
/ / /
Public class RedisHelper
{
/ / /
/ / connection string
/ / /
Private static readonly string ConnectionString = ConfigurationManager.AppSettings ["RedisConnectionString"]
/ / /
/ lock
/ / /
Private readonly object _ lock = new object ()
/ / /
/ connection object
/ / /
Private volatile IConnectionMultiplexer _ connection
/ / /
/ / Database
/ / /
Private IDatabase _ db
Public RedisHelper ()
{
_ connection = ConnectionMultiplexer.Connect (ConnectionString)
_ db = GetDatabase ()
}
/ / /
/ get the connection
/ / /
/ / /
Protected IConnectionMultiplexer GetConnection ()
{
If (_ connection! = null & & _ connection.IsConnected)
{
Return _ connection
}
Lock (_ lock)
{
If (_ connection! = null & & _ connection.IsConnected)
{
Return _ connection
}
If (_ connection! = null)
{
_ connection.Dispose ()
}
_ connection = ConnectionMultiplexer.Connect (ConnectionString)
}
Return _ connection
}
/ / /
/ / get the database
/ / /
/ / /
/ / /
Public IDatabase GetDatabase (int? Db = null)
{
/ / return _ connection.GetDatabase (db?-1)
Return GetConnection () .GetDatabase (db?-1)
}
/ / /
/ Settings
/ / /
/ key
/ / value
/ time
Public virtual void Set (string key, object data, int cacheTime)
{
If (data = = null)
{
Return
}
Var entryBytes = Serialize (data)
Var expiresIn = TimeSpan.FromMinutes (cacheTime)
_ db.StringSet (key, entryBytes, expiresIn)
}
/ / /
/ / get the value according to the key
/ / /
/ / /
/ / /
/ / /
Public virtual T Get (string key)
{
Var rValue = _ db.StringGet (key)
If (! rValue.HasValue)
{
Return default (T)
}
Var result = Deserialize (rValue)
Return result
}
/ / /
/ / deserialization
/ / /
/ / /
/ / /
/ / /
Protected virtual T Deserialize (byte [] serializedObject)
{
If (serializedObject = = null)
{
Return default (T)
}
Var json = Encoding.UTF8.GetString (serializedObject)
Return JsonConvert.DeserializeObject (json)
}
/ / /
/ / determine whether it has been set up
/ / /
/ / /
/ / /
Public virtual bool IsSet (string key)
{
Return _ db.KeyExists (key)
}
/ / /
/ / Serialization
/ / /
/ / /
/ byte []
Private byte [] Serialize (object data)
{
Var json = JsonConvert.SerializeObject (data)
Return Encoding.UTF8.GetBytes (json)
}
}
}
Thank you for your reading, the above is the content of "simple usage of redis database", after the study of this article, I believe you have a deeper understanding of the simple usage of redis database, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.