Example of using Redis in Redis Series 2-C,
The previous Redis series talked about downloading and installing Redis. The next article focuses on usingServiceStack. RedisThis development library is used as a simple example of the cache service in the C # project. The code can be directly written without much talk.
Private void TestRedis () {var Redis = new RedisClient ("localhost"); // create a Redis instance (the host name can be configured in the form of a configuration file based on the actual situation of the project) var records = new List <TestInfo> ();/****** operate on generic data *******/if (Redis. exists ("TestInfos") <= 0) // judge whether a cache Exists {// Add a generic set of data Redis. set ("TestInfos", new List <TestInfo> () {new TestInfo () {id = 1, name = "11"}, new TestInfo () {id = 2, name = "22"}, new TestInfo () {id = 3, name = "33"}, new TestInfo () {id = 4, name = "44"}); Redis. expire ("TestInfos", 60); // set to one minute expired} records = Redis. get <List <TestInfo> ("TestInfos"); // obtain Redis cache data // Redis. remove ("TestInfos"); // delete a cache // records = Redis. get <List <TestInfo> ("TestInfos"); // try to Get it again after deletion. The result is null (null is returned when a nonexistent reference type is obtained in Redis) /****** perform operations on generic data ******* // ***************/var testRedisList = Redis. as <TestInfo> (); IRedisList <TestInfo> testData = testRedisList. lists ["testData"]; // specify a linked list of Redis. expire ("testData", 60); // set to one minute expired testData. addRange (records); // Add testRedisList data. save (); // Save the Linked List data var testDataList = Redis. as <TestInfo> (). lists ["testData"]. toList (); // retrieve linked list data/************** // ****** operate int type data *******/if (Redis. exists ("TestInt") <= 0) {Redis. set ("TestInt", 11); // save an int type of slow data} var recordStr = Redis. get <int> ("TestInt"); // obtain Redis data. remove ("TestInt"); // delete a cache var redisIntVal = Redis. get <int> ("TestInt"); // try to Get it again after deletion, and the result is 0 (default value is returned when Redis obtains a value type that does not exist) /***** operate int-type data *******/}