Example of using Redis in Redis Series 2-C,

Source: Internet
Author: User

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 *******/}
 

  

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.