Servicestack. redis tutorial

Source: Internet
Author: User
Tags redis server redis tutorial

Environment preparation

    • Redis (use the Windows version for testing, and the operating environment is recommended to use the Linux Version)
    • Servicestack. Redis-v3.00

Running the redis server on Windows for development and testing is good, but it is reliable in the operating environment or the Linux version. Next we will extract redis to a directory:

RunRedis-server.exeSee the following Windows console:

As shown above, the port running on redis is 6372.

Let's take a look at the redis client console and run it in the same directory.Redis-cli.exeAnother console will pop up.Program, Refer to try redis tutorial to start your interaction journey.

Enter the command set car. Make "Ford" and add a car. Make as the key. The value is Ford's data and enters redis. Enter the command get car. Make to retrieve Ford.

Next let's go to the topic to talk about servicestack. redis:

Create a console program first, then decompress servicestack.redis-v3.00.zip, and add the following four references.

    • Servicestack. Common
    • Servicestack. Interfaces
    • Servicestack. redis
    • Servicestack. Text

Let's write something belowCodeCreate a car class and store several instances to redis, and then let an object expire after 5 seconds. wait 6 seconds and then output the number of car instances.

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using servicestack. redis;
Using system. Threading;

Namespace redistutorial
{
Class Program
{
Static void main (string [] ARGs)
{
VaR redisclient = new redisclient ("localhost ");

Using (VAR cars = redisclient. gettypedclient <car> ())
{
If (cars. getall (). Count> 0)
Cars. deleteall ();

VaR dansford = new car
{
Id = cars. getnextsequence (),
Title = "Dan's Ford ",
Make = new make {name = "Ford "},
Model = new model {name = "Fiesta "}
};
VaR beccisford = new car
{
Id = cars. getnextsequence (),
Title = "becci's Ford ",
Make = new make {name = "Ford "},
Model = new model {name = "Focus "}
};
VaR vauxhallastra = new car
{
Id = cars. getnextsequence (),
Title = "Dans Vauxhall Astra ",
Make = new make {name = "Vauxhall "},
Model = new model {name = "Asta "}
};
VaR vauxhallnova = new car
{
Id = cars. getnextsequence (),
Title = "Dans Vauxhall Nova ",
Make = new make {name = "Vauxhall "},
Model = new model {name = "NOVA "}
};

VaR carstostore = new list <car> {dansford, beccisford, vauxhallastra, vauxhallnova };
Cars. storeall (carstostore );

Console. writeline ("redis has->" + cars. getall (). Count + "cars ");

Cars. expireat (vauxhallastra. ID, datetime. Now. addseconds (5); // expire Vauxhall Astra in 5 seconds

Thread. Sleep (6000); // wait 6 seconds to prove we can expire our old Astra

Console. writeline ("redis has->" + cars. getall (). Count + "cars ");

// Get cars out of redis
VaR carsfromredis = cars. getall (). Where (CAR => Car. Make. Name = "Ford ");

Foreach (VAR car in carsfromredis)
{
Console. writeline ("redis has a->" + car. Title );
}

}
Console. Readline ();
}
}

Public class car
{
Public long ID {Get; set ;}
Public String title {Get; set ;}
Public String description {Get; set ;}
Public make {Get; set ;}
Public model {Get; set ;}
}

Public class make
{
Public int ID {Get; set ;}
Public string name {Get; set ;}
}

Public class model
{
Public int ID {Get; set ;}
Public make {Get; set ;}
Public string name {Get; set ;}
}

}

 

Download the sample code: redistutorial.zip

Problems and fixes of servicestack. redis

Performance testing: redis performance testing of tens of millions of data volumes

Several suggestions to make redis play a greater role in your system

Redis

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.