Environment preparation
- Redis (tested with Windows version, Linux version recommended for operational environments)
- servicestack.redis-v3.00
It's good to run a Redis server on Windows for development and testing, but in an operating environment or Linux version, let's extract Redis to a directory:
Run Redis-server.exe See the following Windows console:
Above we can see that the Redis run port is 6372
Let's play with the Redis client console and run Redis-cli.exe in the same directory, and another console program will pop up, and you can refer to try Redis Tutorial to start your interactive journey.
Enter command set Car.make "Ford" added a car.make for Key,value is Ford's data into Redis, enter command get Car.make can retrieve Ford
Below we enter the topic, the protagonist Servicestack.redis:
First create a console program, then unzip Servicestack.redis-v3.00.zip, and then add the following four references
- Servicestack.common
- Servicestack.interfaces
- Servicestack.redis
- Servicestack.text
Let's write some code below, create a car class and store a few instances to Redis, then let an object expire after 5 seconds, wait 6 seconds to output the number of instances of car
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-and" + car.) Title);
}
}
Console.ReadLine ();
}
}
public class Car
{
Public long Id {get; set;}
public string Title {get; set;}
public string Description {get; set;}
Public make make {get; set;}
Public model 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 make {get; set;}
public string Name {get; set;}
}
}
Example code download: Redistutorial.zip
The problem and correction of Servicestack.redis
Performance testing: Performance testing of data volumes for Redis tens
A few suggestions to make Redis play a bigger role in your system
Redis
Servicestack.redis use