Servicestack.redis use

Source: Internet
Author: User
Tags redis server redis tutorial

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

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.