Introduction:Mainly introduces the caching middlewareMemCachedcap (a poem)Redis
MemCached
1、Introduction to MemCached
MemCached is a memory-based key-value store for storing small chunks of arbitrary data (strings, objects). It facilitates rapid development, eases development, and solves many of the problems of caching large amounts of data , in essence, it is a concise key-value storage system
2、MemCached working principle
Mainly by caching database query results and reducing the number of database accesses to improve the speed of dynamic Web applications. See the figure below:
Redis
1、Redis Introduction
Redis is completely open source and free and is a high performance key-value database.
Redis has three characteristics that set it apart from other key - value caching products:
(1) Redis supports data persistence, which allows you to save data in memory to disk and load it again for use when you restart.
(2) Redis supports the storage of data structures such as String, list, set, zset, and hash.
(3) Redis supports data backup, i.e. master-slave mode data backup.
2、LinuxInstall Redis under
Download Address:/download
(1) Download and install:
$ wget http://download.redis.io/releases/redis-4.0.10.tar.gz
$ tar xzf redis-4.0.10.tar.gz
$ cd redis-4.0.10
$ make
- 1
- 2
- 3
- 4
(2) Start the redis service
$ cd src
$ ./redis-server
- 1
- 2
(3) Using the redis client
$ cd src
$ ./redis-cli
redis> set companyName G7
OK
redis> get companyName
"G7"
- 1
- 2
- 3
- 4
- 5
- 6
3, Java using Redis
import ;
import .*;
/**
* Created by pc on 2018/7/23.
* Redisdata type
*/
public class RedisDemo
{
//String
public static void redisString(){
//Connecting to the Redis Service
Jedis jedis = new Jedis("172.16.*.**",6379);
System.out.println("Connection successful.");
// Check if the service is running
System.out.println("Server is running: " + ());
// Setting up redis string data
jedis.set("runoobkey", "");
// Get the stored data and output it
System.out.println("The string stored by redis is: "+ jedis.get("runoobkey"));
}
//list
public static void redisList(){
//Connecting to the Redis Service
Jedis jedis = new Jedis("172.16.*.**",6379);
System.out.println("Connection successful.");
// Storing data in a list
("site-list", "Baidu");
("site-list", "Google");
("site-list", "Taobao");
// Get the stored data and output it
List<String> list = ("site-list", 0 ,2);
for(int i=0; i<(); i++) {
System.out.println("The list item is: "+list.get(i));
}
}
//set
public static void redisSet(){
//Connecting to the Redis Service
Jedis jedis = new Jedis("172.16.*.**",6379);
System.out.println("Connection successful.");
//Deposit the value of set
Long mySet = ("websites", "Baidu", "Taobao", "Google");
//get the value of set
Set<String> website = ("websites");
Iterator<String> it = ();
while(()){
System.out.println(());
}
}
//hash
public static void redisHash(){
//Connecting to the Redis Service
Jedis jedis = new Jedis("172.16.*.**",6379);
System.out.println("Connection successful.");
Map<String, String> hashMap = new HashMap<>();
("Baidu","");
("Taobao","");
("Google","");
("website", hashMap);
System.out.println(("website"));
}
public static void main(String[] args) {
redisString();
// redisList();
// redisSet();
// redisHash();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
After running, the following figure is shown:
4, Redis caching strategy
(1) Cache [invalidation]: the client requests data from the cache first, if not then query the database, and finally put the data into the cache
(2) Cache [hit]: the client fetches the data directly from the cache and returns the result
(3) Cache [update]: the client writes data to the database, and after it succeeds, it invalidates the cache (the next time the request does not get it from the cache, the database is queried, and it is put back into the cache)
5. Several problematic update cache strategies
(1) Update the cache first and then the DB. see the following figure:
As you can see from the figure, two concurrent write operations, due to some reasons (io blocking, cpu time slice allocation, concurrent scheduling, network reasons, etc.), resulting in Thread2's update DB is later than Thread1's update DB, but Redis at this point in time the data of Thread1, while the data in the DB when the data of Thread2, which gives rise to the inconsistency problem, the DB is dirty data
(2) Update the DB first, then the cache. See the figure below:
As you can see from the figure, two concurrent write operations, due to some reasons lead to Thread2's update Redis later than Thread1's update Redis, but the DB at this time the data of Thread1, while the data in the Redis when Thread2, which is inconsistent problem!
(3) Delete the cache first and then update the database. See the figure below:
Two concurrent operations, one is an update operation and the other is a query operation. after the update operation deletes the cache, the query operation doesn't hit the cache, it reads out the old data and puts it into the cache, and then the update operation updates the DB. so the data in the cache is still old, resulting in a dirty cache.
(4) Database first, after success, let the cache invalidate, the next time the request can not be taken from the cache, then query the database, and then put into the cache. See the figure below:
This update strategy is the one we actually use most often, but it can be problematic. The probability of a problem is actually very low, because the condition requires that the cache be invalidated during a read and that there be a concurrent write operation. In reality, the database write operation will be much slower than the read operation, and also lock the table, and the read operation must enter the database operation before the write operation, and later than the write operation to update the cache, the probability that all these conditions are met is basically not very high.
6. The difference between Redis and MemCached
(1) Redis and Memcache both store data in memory, both are in-memory databases. But memcache can also be used to cache other things like images, videos, etc;
(2) Redis not only supports simple k/v type data, but also provides list, set, hash and other data structures for storage;
(3) Virtual memory-Redis can swap some long-unused values to disk when physical memory runs out;
(4) Distributed cluster deployment:
a, memcache cluster nodes are independent of each other's data, can not communicate with each other, but can be solved by using magent open source software ;)
b, Redis highly available, you can do a master multi-slave, master-slave data synchronization. When the Master is down, the election algorithm (Paxos, Raft) from the slave to elect a new Master to continue to provide services to the outside world, the host recovery to rejoin the identity of the slave
(5) Storage data security - memcache hangs, the data is gone; redis can be periodically saved to disk (persistent);
(6) Disaster recovery - memcache hangs, the data can not be recovered; redis data loss can be recovered through the aof;
...... end