A. What is Redis?
Redis is a key-value storage system. Similar to memcached, it supports storing more value types, including string (string), list (linked list), set (set), Zset (sorted set-ordered collection), and hash (hash type). These data types support Push/pop, Add/remove, and intersection-set and difference sets, and richer operations, and these operations are atomic. Based on this, Redis supports sorting in a variety of different ways. As with memcached, data is cached in memory to ensure efficiency. The difference is that Redis periodically writes the updated data to disk or writes the modified operation to the appended record file, and Master-slave (Master-Slave) synchronization is implemented on this basis. Redis is a high-performance Key-value database. The emergence of Redis, to a large extent, compensates for the lack of memcached such key/value storage, in some cases can be a good complement to the relational database. It provides clients such as Java,c/c++,c#,php,javascript,perl,object-c,python,ruby,erlang, which is convenient to use. Redis supports master-slave synchronization. Data can be synchronized from the primary server to any number of slave servers, from the server to the primary server that is associated with other slave servers. This enables Redis to perform single-layer tree replication. You can write to the data intentionally or unintentionally. Because of the full implementation of the publish/subscribe mechanism, you can subscribe to a channel and receive a complete message release record from the master server when the tree is synchronized anywhere from the database. Synchronization is helpful for the scalability and data redundancy of read operations. (quoted from Baidu Encyclopedia)two. Why use Redis
Redis is a remote in-memory database that not only has strong performance, but also has replication features and a unique data model for solving problems. Redis provides 5 different types of data structures, all of which can be easily mapped to these data structures: Redis's data structures are designed to help users solve problems without requiring users to distort the problem to fit the database, as other databases do. In addition, features such as replication, persistence (persistence), and client-side sharding (client-side sharding) make it easy for users to extend Redis into a system that can contain hundreds of GB of data and process millions of requests per second.
Using Redis can greatly improve the performance of our application, such as a novel website, need to display the top 10 reading volume of the novel, if every time to go from the database query is too expensive database performance, will increase our response time. We can put this list in Redis, so that each time we can get the amount of data from the Redis, the speed is very fast.
Three. Aspnet-zero-core how to use the Redis cache
Project "MyCompanyName.MyProject.Web.Core" hereinafter referred to as "Web.core"
Project "MYCOMPANYNAME.MYPROJECT.WEB.MVC" hereinafter referred to as "WEB.MVC"
Using the Redis cache, first install the package through NuGet Abp.rediscache
1. Open the "AbpZeroTemplateWebCoreModule.cs" of the Project "Web.core" in the "Preinitialize" method
1 Configuration.Caching.UseRedis (options =2 {3 options. ConnectionString = _appconfiguration["Abp:RedisCache:ConnectionString"]; 4 Options. DatabaseId = _appconfiguration.getvalue<int> ("Abp:RedisCache:DatabaseId " ); 5 });
2. Modify Redis Configuration profile in Project "WEB.MVC" Appsettings.json
Here's the DatabaseID, and the Redis database ID, different database IDs help create different key spaces (isolated caches) in the same server.
3. Set the default expiration time, also in the project "Web.core" "AbpZeroTemplateWebCoreModule.cs" in the "Preinitialize" method, placed before the Redis code
1 //Set default expiration time for all caches2Configuration.Caching.ConfigureAll (cache =3 {4Cache. Defaultabsoluteexpiretime = Timespan.fromminutes (2);5 });6 //set the default expiration time for a cache according to "CacheName" to differentiate7Configuration.Caching.Configure ("CacheName", cache =8 {9Cache. Defaultabsoluteexpiretime = Timespan.fromminutes (2);Ten});
4. Finally, do not forget to set the module dependencies
Four. Verify that the setting is successful
Start the run project to see if there is data in the Redis. I'm using redisdesktopmanager,:https://redisdesktop.com/download here.
It is clear that the use has been successful.
Five. How to use caching in your code
1. Inject the cache manager through a container, where you can use attribute injection or construct method injection to
2. Using the cache
Here the Get method has two parameters, the first parameter is the key in the cache, the second parameter is when the data in the cache does not exist, then execute the method of acquiring the data.
Think : We all know that Redis is a cross-process or even cross-domain cache, and sometimes we don't need to use the Redis cache, which is enough to use the native cache, and I think there's bound to be a use of Redis cache and native cache as MemoryCache.
The ABP should not support the existence of multiple caches by default, at least for the moment I don't see them in official documents. We look at the source code of the Useredis method that enables the Redis cache in that few sentences:
Can be found in the red box injected Abprediscachemanager, and then imagine that we use the cache when the container injection is the Redis cache, Then we just need to inject memorycache into the same time using Redis and MemoryCache.
This is just my idea, and not tested, interested friends can test it.
Okay, the setup of Redis is very simple, and the next one will share the pits that I used to store hangfire through the MySQL database in the core ~
If you feel good writing, please click on the " recommended " below, this is to share my technical experience support, thank you! ↓↓↓↓↓↓↓↓↓↓↓
ABP from Getting Started to mastering (3): Aspnet-zero-core using Redis cache