Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Redis in .NET

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

How to use Redis in .NET? I believe that many novice rookies have not yet learned this skill. Through the summary of this article, I hope you can learn this skill. The following materials are the steps for implementation.

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

Recently, you need to use Redis in your project. Here is a brief account of the installation of Redis and how to use Redis in .NET.

Redis installation and startup

1. Download Redis

Redis itself does not provide Windows version, and it is not stable on Windows. It is generally deployed to Linux environment. Redis can be downloaded on its official website, and Windows version is provided in MSOpenTech. Here to learn to install this version.

After clicking to jump to Github, click Zip to download directly. After download, choose 32-bit or 64-bit to install according to the version of your computer. I decompress the 64-bit file and put it under the D:\ Redis folder. At the same time, I copy the redis.conf in the folder to this directory. This is the configuration information of redis:

two。 Start Redis

Enabling Redis under Windows is the same as starting MogoDB, you need to start using the command line. First, navigate to this directory and run the following command:

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

Because it is running locally, you should pay attention to the port number and keep the port open.

Of course, you can always enable Redis as a Windows service in the background.

3. Use

Now open another console application to connect to the Redis you started before, as follows:

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

Where-h is followed by the local ip address, followed by the port.

You can then execute set to assign a value to key to city:

Redis 172.16.147.121 6379 > set city Shanghai

You can get the value that specifies key as city through get.

Redis 172.16.147.121 6379 > get city

At the same time, when we write data to redis, the Redis service also writes data to files regularly.

. Preliminary study on Redis

Download ServiceStack.Redis

Like MongoDB, using Redis in .NET actually uses a third-party driver. The official website recommends using ServiceStack.Redis to download and decompress to get the following dll

Using Redis in .NET projects

Create a new Console program and reference the four dll extracted in the previous step.

For a simple example, get the value of the city we set earlier in .NET.

Class Program {static RedisClient redisClient = new RedisClient ("172.16.147.121", 6379); / / redis service IP and port static void Main (string [] args) {Console.WriteLine (redisClient.Get ("city")); Console.ReadKey ();}}

First pass static RedisClient redisClient = new RedisClient ("172.16.147.121", 6379)

Establish a connection, and then you can directly use the Get method in redisClient to get the value of key as city.

In the previous command line, we stored Shanghai in our network city, and now we have this value.

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

After reading the above, have you mastered the method of using Redis in .NET? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report