Home > Database > Redis > Introduction to methods of using Redis in .NET

Introduction to methods of using Redis in .NET

Release: 2019-12-04 17:22:05
forward
2790 people have browsed it

Introduction to methods of using Redis in .NET

Redis is a widely used Key/Value in-memory database. It is used as a cache in large applications such as Sina Weibo, Github, and StackOverflow. (Recommended: redis tutorial)

Recent projects require the use of Redis. Here is a brief record of the installation of Redis and how to use Redis in .NET.

Redis installation and startup

1. Download Redis

Redis itself does not provide a Windows version, and it is not stable on Windows. It is generally deployed to Linux. Environment, Redis can be downloaded from its official website. MSOpenTech provides a Windows version. Here you can learn to install this version.

Introduction to methods of using Redis in .NETAfter clicking to jump to Github, click Zip to download directly. After downloading, choose 32-bit or 64-bit according to the version of your computer to install. I decompressed the 64-bit version and put it in the D:\Redis folder, and also copied the redis.conf in the folder to the directory. This is the configuration information of redis:

Introduction to methods of using Redis in .NET

2. Start Redis

Enabling Redis under Windows is the same as starting MogoDB. You need to use the command line to start. First locate the directory and run the following command:

D:\ Redis>redis-server.exe redis.conf

Introduction to methods of using Redis in .NET

#Because it is running on the local machine, pay attention to the port number here, and keep the port closed.

Of course, you can also keep Redis open in the background as a Windows service.

3. Use

Now open a console application to connect to the previously started Redis, as follows:

D:\Redis>redis-cli.exe -h 172.16 .147.121 -p 6379

Where –h is followed by the IP address of the machine, followed by the port.

Then you can execute set to assign the key to city:

redis 172.16.147.121:6379> set city Shanghai

You can get the specified key as city through get It's worth it.

redis 172.16.147.121:6379> get city

Introduction to methods of using Redis in .NET

##At the same time, when we write data to redis, the Redis service will also be scheduled Write data to the file

Introduction to methods of using Redis in .NET

. Preliminary exploration of Redis

Download ServiceStack.Redis

is the same as MongoDB, Using Redis in .NET actually also uses a third-party driver. The official website recommends using ServiceStack.Redis. After downloading and decompressing, you will get the following dll

Introduction to methods of using Redis in .NET

Use Redis## in the .NET project

#Create a new Console program and reference the four dlls decompressed in the previous step.

Let’s do a simple example to get the city value we set before in .NET.

class Program
{
    static RedisClient redisClient = new RedisClient("172.16.147.121", 6379);//redis服务IP和端口
    static void Main(string[] args)
    {
        Console.WriteLine(redisClient.Get<string>("city"));
        Console.ReadKey();
    }
}</string>
Copy after login

First establish a connection through static RedisClient redisClient = new RedisClient("172.16.147.121", 6379);

, and then you can directly use the Get method in redisClient to obtain the value with key city. .

Introduction to methods of using Redis in .NETIn the previous command line, Shanghai was stored in our network city, and now we have obtained this value.

There are many methods in ServerStack that can be called in .NET. The class structure diagram is as follows:

The above is the detailed content of Introduction to methods of using Redis in .NET. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template