Example of using Redis in Redis series 2-c#

Source: Internet
Author: User
Tags redis download

Tag: Set Service a color list default value CSharp composite BSP

The previous Redis series has already talked about Redis download, installation, the next one, mainly using the Servicestack.redis provided by Redis, a simple example of how the development library is used as a caching service in a C # project, with little nonsense, directly on the code.

        private void Testredis () {var Redis = new Redisclient ("localhost");//Create Redis instance (hostname based on the actual project settings,            Can be configured in the form of a configuration file) var records = new List<testinfo> ();                 /***** operation generic Data *******/if (redis.exists ("Testinfos") <= 0)//Determine if a cache exists {//Add generic collection data Redis.set ("Testinfos", new List<testinfo> () {new TestInfo () {ID  = 1, name = "One"}, new TestInfo () {id = 2, name = "$"}, new TestInfo () {id = 3,                Name = ""}, New TestInfo () {id = 4, name = "44"}}); Redis.expire ("Testinfos", 60); Set to one-minute expiration} records = Redis.get<list<testinfo>> ("Testinfos"); Gets the Redis cache data//redis.remove ("Testinfos"); Delete a cache//records = redis.get<list<testinfo>> ("Testinfos"); Attempted re-fetch after deletion, result is null (returns NULL when a non-existent reference type is obtained in Redis)/*Operation generic data *******//***** linked list Operation *******/var testredislist = redis.as<testinfo> (); iredislist<testinfo> testData = testredislist.lists["TestData"];  Specify a list of Redis.expire ("TestData", 60); Set to one-minute expiration Testdata.addrange (records); Add Data Testredislist.save (); Save linked list data var testdatalist = redis.as<testinfo> (). lists["TestData"]. ToList ();             Get list data/***** linked list operations *******//***** operations int type data *******/if (redis.exists ("Testint") <= 0) {Redis.set ("Testint", 11);//Save slowly data of type int} var recordstr = redis.get& Lt;int> ("Testint"); Obtain Data Redis.remove ("Testint"); Delete a cache var redisintval = redis.get<int> ("Testint"); Attempted re-fetch after deletion, result is 0 (default value is returned when Redis gets a nonexistent value type)/***** operation int type data *******/}

  

Example of using Redis in Redis series 2-c#

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.