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