Redis can now be started, and Redis has only one boot parameter, which is his profile path.
Using system;using system.collections.generic;using system.linq;using system.text;using ServiceStack.Redis;using Servicestack.redis.support;namespace redisstudy{class Program {static void Main (string[] args) { try {//Get Redis operator interface Iredisclient Redis = Redismanager.getclient (); Hash table operation Hashoperator operators = new Hashoperator (); Remove a cached data bool IsTrue = Redis.remove ("Additemtolist"); Add a list of strings to Redis list<string> storemembers = new list<string> () {"Han Meimei", "Li Lei", "Lucy"}; Storemembers.foreach (x = redis.additemtolist ("Additemtolist", x)); Gets the value set corresponding to the specified key Console.WriteLine ("Gets the value set corresponding to the specified key:"); var members = redis.getallitemsfromlist ("Additemtolist"); Members. ForEach (s = = Console.WriteLine ("Additemtolist:" + s)); Console.WriteLine (""); Gets the specified index location data Console.WriteLine ("Get specified index location data:"); var item = redis.getitemfromlist ("Additemtolist", 2); Console.WriteLine (item); Console.WriteLine (""); Console.WriteLine The data into the hash table ("Hash table data storage:"); UserInfo Userinfos = new UserInfo () {UserName = "Li lei", age = 45}; var ser = new Objectserializer (); Located in namespace ServiceStack.Redis.Support; BOOL results = operators. Set<byte[]> ("Userinfoshash", "Userinfos", Ser. Serialize (Userinfos)); byte[] Infos = operators. Get<byte[]> ("Userinfoshash", "Userinfos"); Userinfos = ser. Deserialize (infos) as UserInfo; Console.WriteLine ("Name=" + userinfos.username + "age=" + userinfos.age); Console.WriteLine (""); The object is serialized in a way that stores the Console.WriteLine ("Object is serialized as stored:"); UsErinfo uinfo = new UserInfo () {UserName = "Zhang san", age = 12}; BOOL result = redis.set<byte[]> ("Uinfo", Ser. Serialize (Uinfo)); UserInfo Userinfo2 = ser. Deserialize (redis.get<byte[]> ("Uinfo")) as UserInfo; Console.WriteLine ("Name=" + Userinfo2. UserName + "age=" + Userinfo2. Age); Console.WriteLine (""); Store value type Data Console.WriteLine ("Store Value type data:"); Redis.set<int> ("My_age", 12);//or Redis.set ("My_age", 12); int age = redis.get<int> ("My_age"); Console.WriteLine ("age=" + age); Console.WriteLine (""); Serialized list Data Console.WriteLine ("List data:"); list<userinfo> userinfolist = new List<userinfo> {new userinfo{username= "Lucy", Age=1,id=1}, New Userinfo{username= "Mary", age=3,id=2},}; Redis.set<byte[]> ("Userinfolist_serialize", Ser. Serialize (userinfolist)); list<userinfo> userlist = ser. Deserialize (redis.get<byte[]> ("Userinfolist_serialize")) as list<userinfo>; Userlist.foreach (i + = {Console.WriteLine ("Name=" + i.username + "age=" + i.age); }); Free memory redis.dispose (); Operators. Dispose (); Console.readkey (); } catch (Exception ex) {Console.WriteLine (ex. Message.tostring ()); Console.WriteLine ("Please open the Redis-server.exe"); Console.readkey (); }}}}</span>
Redismanager class:
Using system;using system.collections.generic;using system.linq;using system.text;using ServiceStack.Redis; namespace redisstudy{//<summary>///Redismanager class is primarily A///</summary> public class Red that creates a linked pool management object Ismanager {//<summary>///Redis profile information///</summary> private static string Redispath = system.configuration.configurationsettings.appsettings["Redispath"]; private static Pooledredisclientmanager _PRCM; <summary>///Static construction method, initialize the link Pool Management object///</summary> static Redismanager () { Createmanager (); }///<summary>//Create a linked Pool management object///</summary> private static void Createmanager () {_PRCM = Createmanager (new string[] {Redispath}, new string[] {redispath}); } private static Pooledredisclientmanager Createmanager (string[] readwritehosts, string[] readonlyhosts) { Writeserverlist: A writable Redis link address. Readserverlist: A readable redis link address. Maxwritepoolsize: Maximum number of write links. Maxreadpoolsize: Maximum number of Read links. AutoStart: Automatic restart. Localcachetime: Local cache expiry time in seconds. Recordelog: Log is logged, this setting is only used to troubleshoot Redis runtime issues, such as Redis working properly, close the item. The Redisconfiginfo class is a recording of Redis connection information, which echoes the Redisconfig in the configuration file//supports read/write separation, and balances load return new pooledredisclient Manager (Readwritehosts, readonlyhosts, new Redisclientmanagerconfig {maxwritepoolsize = 5,//" Write "link pool link Number maxreadpoolsize = 5,//" read "link pool link Number AutoStart = true,}); } private static Ienumerable<string> splitstring (String strsource, String split) {return S Trsource.split (Split. ToArray ()); }///<summary>//Client Cache Action Object///</summary> public static iredisclient getclient ( ) {if (_PRCM = = null) {Createmanager (); } return _PRCM. Getclient (); }}}</span>
Redisoperatorbase class:
Using system;using system.collections.generic;using system.linq;using system.text;using ServiceStack.Redis; namespace redisstudy{//<summary>///Redisoperatorbase class, which is the base class for Redis operations, inherits from the IDisposable interface, primarily for freeing memory//</S Ummary> public abstract class Redisoperatorbase:idisposable {protected iredisclient Redis {get; privat e set; } private bool _disposed = false; Protected Redisoperatorbase () {Redis = Redismanager.getclient (); } protected virtual void Dispose (bool disposing) {if (!this._disposed) { if (disposing) {redis.dispose (); Redis = null; }} this._disposed = true; public void Dispose () {Dispose (true); Gc. SuppressFinalize (this); }///<summary>//Save data DB file to hard drive///</summary> public VOID Save () {redis.save (); }///<summary>///asynchronously saves the data db file to hard disk//</summary> public void Saveasync () { Redis.saveasync (); }}}</span>
Hashoperator class:
Using system;using system.collections.generic;using system.linq;using system.text;using ServiceStack.Text;namespace redisstudy{//<summary>//Hashoperator class, is the Operation hash table class. Inherit from Redisoperatorbase class///</summary> public class Hashoperator:redisoperatorbase {public hashoper Ator (): Base () {}///<summary>///To determine if a data has been cached///</summary> public bool Exi St<t> (string Hashid, string key) {return redis.hashcontainsentry (Hashid, key); }///<summary>///store data to hash table///</summary> public bool Set<t> (string hash Id, string key, T t) {var value = jsonserializer.serializetostring<t> (t); Return Redis.setentryinhash (Hashid, key, value); }///<summary>///Remove a value from the hash///</summary> public bool Remove (string Hashid, St Ring key) {return REDIS.REMOVEENTRYFRomhash (Hashid, key); }///<summary>//Remove entire hash///</summary> public bool Remove (string key) {return redis.remove (key); }///<summary>///For data from the hash table///</summary> Public T get<t> (string Hashid, String key) {String value = Redis.getvaluefromhash (Hashid, key); return jsonserializer.deserializefromstring<t> (value); }///<summary>//Get the entire hash data///</summary> public list<t> Getall<t> ;(string Hashid) {var result = new list<t> (); var list = redis.gethashvalues (Hashid); if (list! = null && list. Count > 0) {list. ForEach (x = = {var value = jsonserializer.deserializefromstring<t> (x); Result. ADD (value); });} return result; }///<summary>//Set cache expires///</summary> public void Setexpire (string key, Dateti Me datetime) {Redis.expireentryat (key, DateTime); }}}</span>
UserInfo class:
Using system;using system.collections.generic;using system.linq;using system.text;namespace RedisStudy{ [ Serializable] public class UserInfo {public int Id; public string UserName; public int age; }} </span>
App. Config configuration:
<?xml version= "1.0"? ><configuration><startup><supportedruntime version= "v4.0" sku= ". netframework,version=v4.0 "/></startup> <appSettings> <add key=" Redispath "value=" 127.0.0.1:6379 "/> </appSettings></configuration>
The above is an encapsulation class for redis operations, which can be called directly.
Specific code Download:
Redis Code