Combining the Redis-Windows version of C# with the project under the .Net platform

1. Because Redis does not support Windows platform at present, so if vb.net tutorial

It is a large-scale project, and the best resource is to deploy Redis under the Linux platform.
2. If you use the Redis service in the c# project, you can download the Redis release version provided by the c# tutorial Microsoft
Insert picture description here

1. After downloading and decompressing, try to start it. The command window cannot be PowerShell, otherwise the following interface will appear;
Insert picture description here

2. cd to the current Redis directory, enter **redis-server.exe redis.windows.conf ** to start, and the following interface will appear if successful. (Note: The Redis syntax python basic tutorial is very strict. If there is a problem with the operation, please pay attention to whether there are too many spaces or other characters)
Insert picture description here

3. Then reopen a command window. Cd to the current Redis directory and enter the redis-cli.exe -h 127.0.0.1 -p 6379 command to connect to the service.
4. Add a Key Value.
Insert picture description here

5. Get a Key Value value.
Insert picture description here

*Note: Do not close the command window for starting the Redis service, otherwise the connection will be disconnected and the following problems will occur
Insert picture description here

After the test is successful, we will use it in the project.
Note: Because the service is registered through the command line, and the command window must be closed after executing the command, it can only be used by registering the windows service.
1. Register the Redis service locally: redis-server --service-install redis.service-windows.conf, after the registration is successful, start: redis-server --service-start,
the data stored in the client: set a 1;, it still exists after the service is closed (redis-server --service-stop) and reopened and saved in the .pdb file. (After registering as a windowserver service, you only need to start loading the resource configuration file for the first time and save it in the service Cache, start the service later, there will be data previously stored in the cache)
Uninstall the Redis service: redis-server --service-uninstall
2. Load the configuration file through the RedisConcectionHelp class, get the IP connection, create the connection object, and load the database. Prepare the lock mechanism.

/// <summary>
/// 连接字符串
/// </summary>
private static readonly string ConnectionWriteString = ConfigurationManager.AppSettings["RedisPath"];
/// <summary>
/// redis 连接对象
/// </summary>
private static readonly IConnectionMultiplexer ConnMultiplexer = ConnectionMultiplexer.Connect(ConnectionWriteString);
/// <summary>
/// 锁
/// </summary>
private static readonly object Locker = new object();
/// <summary>
/// 数据库
/// </summary>
private readonly IDatabase _db;

Then you can use the Redis service to read.

Redis multi-service startup configuration:
redis.windows-service.conf or redis.windows.conf
configuration file can be customized, for example: server port is 6379, naming: redis.6379windows.conf Example 2: server port is 6380, Name: redis.6380windows.conf
1. Redis does not run as a daemon by default. You can modify it through this configuration item. Use yes to enable the daemon. "daemonize no" There is no daemon under Windows.
2. Specify the Redis listening port. The default port is port: 6379
3. Bind host address bind 127.0.0.1
4. When the client is idle for how long the connection is closed, if specified as 0, it means to close the function timeout 300
5. Set the number of databases, the default database is 0, You can use the SELECT command to specify the database ID databases 16 on the connection.
6. When specifying the RDB method for persistence, the data will be synchronized to the data file for how long and how many update operations. Multiple conditions can be matched, syntax format Yes: save Redis default configuration file provides three conditions:
save 900 1
save 300 10
save 60 10000
means 1 change in 900 seconds (15 minutes), 10 changes in 300 seconds, and 10000 in 60 seconds. Changes.
7. Specify the local database file name, the default value is dump.rdb (this local database can also be named by yourself) dbfilename dump.rdb
8. Specify whether to log after each update operation. Redis writes data to the disk asynchronously by default. If it is not turned on, it may cause data loss for a period of time when the power is off. Because redis itself synchronizes data files according to the above save conditions, some data will only exist in memory for a period of time. The default is no appendonly no
9. Specify the update log file name, and the default is appendonly.aof appendfilename appendonly.aof (under windows system, don’t worry about it)
10. Specify the update log conditions, there are 3 optional values:
no: means etc. The operating system synchronizes the data cache to the disk (fast)
always: it means manually calling fsync() after each update operation to write the data to the disk (slow, safe)
everysec: means syncing once per second (compromise, default value)
appendfsync everysec
11 , The storage Redis service is turned on and off, and the operation log points to the logfile "server6379_log.txt" in the conf configuration

Guess you like

Origin blog.csdn.net/chinaherolts2008/article/details/112853563