<> one , Multilevel cache

<>1. Traditional caching scheme

* Request arrival tomcat after , Go first redis Get cache from , If you don't hit, go mysql Get in

<>2. Multilevel cache scheme

* tomcat Number of concurrent requests , Is far less than redis of , therefore tomcat Will become a bottleneck
* Use requests to process each link , Add cache separately , ease tomcat pressure , Improve service performance

<> two ,JVM Local cache

* The cache is stored in memory , Fast data reading speed , It can greatly reduce the access to the database , Reduce database pressure Distributed cache , as redis - advantage :
Large storage capacity , Good reliability , Can be shared in a cluster - shortcoming : Accessing the cache has network overhead - scene : Large amount of cached data , High reliability , Data to be shared in the cluster Process local cache ,
as HashMap, GuavaCache - advantage : Read local memory , No network overhead , Faster - shortcoming : Limited storage capacity , Low reliability ( If lost after restart ), Cannot share in cluster
- scene : High performance requirements , Less cached data
<>1. Practical cases

* Caffeine Is based on java8 Developed , High performance local cache library with near optimal hit rate
* at present spring This is what the internal cache uses <dependency> <groupId>com.github.ben-manes.caffeine</
groupId> <artifactId>caffeine</artifactId> <version>3.0.5</version> </dependency
> package com.erick.cache; import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine; import java.time.Duration;
public final class CacheUtil { private static int expireSeconds = 2; public
static Cache<String, String> cacheWithExpireSeconds; private static int maxPairs
= 1; public static Cache<String, String> cacheWithMaxPairs; static {
/* Expiration Policies , finish writing sth. 60s Post expiration */ cacheWithExpireSeconds = Caffeine.newBuilder() .
expireAfterWrite(Duration.ofSeconds(expireSeconds)) .build(); /* Expiration Policies , Delete when the maximum value is reached *
1. It will not be deleted immediately , It won't be deleted until later * 2. The previously stored data will be deleted */ cacheWithMaxPairs = Caffeine.newBuilder(
) .maximumSize(maxPairs) .build(); } /* Get data from cache * 1. If in cache , Is returned directly from the cache * 2.
If not in the cache , Then go to the data query and return the results */ public static String getKeyWithExpire(String key) { return
cacheWithExpireSeconds.get(key, value -> { return getResultFromDB(); }); }
public static String getKeyWithMaxPair(String key) { return cacheWithMaxPairs.
get(key, value -> { return getResultFromDB(); }); } private static String
getResultFromDB() { System.out.println(" Database query "); return "db result"; } } package
com.erick.cache; import java.util.concurrent.TimeUnit; public class Test { @org
.junit.Test public void test01() throws InterruptedException { CacheUtil.
cacheWithExpireSeconds.put("name", "erick"); System.out.println(CacheUtil.
getKeyWithExpire("name")); TimeUnit.SECONDS.sleep(3); System.out.println(
CacheUtil.getKeyWithExpire("name")); } @org.junit.Test public void test02()
throws InterruptedException { CacheUtil.cacheWithMaxPairs.put("name", "erick");
CacheUtil.cacheWithMaxPairs.put("age", "12"); System.out.println(CacheUtil.
getKeyWithMaxPair("name")); System.out.println(CacheUtil.getKeyWithMaxPair("age"
)); TimeUnit.SECONDS.sleep(2); System.out.println(CacheUtil.getKeyWithMaxPair(
"name")); // I can't find it System.out.println(CacheUtil.getKeyWithMaxPair("age")); } }
<> three , Cache consistency

<>1. Common scheme

<>1.1 Set validity period

* Set the validity period for the cache , Automatically delete after expiration . It can be updated when querying again
* advantage : simple , convenient
* shortcoming : Poor timeliness , The cache may be inconsistent before it expires
* scene : Low update frequency , Businesses with low timeliness requirements
<>1.2 Synchronous double write

* While modifying the database , Modify cache directly
* advantage : Code intrusion , Strong consistency between cache and database
* shortcoming : Code entry , High coupling
* scene : Pair consistency , Cache data with high invalidity requirements
<>1.3 Asynchronous notification

* Send event notification when modifying database , Modify the cached data after the relevant service listens to it
* advantage : Low coupling , Multiple cache services can be notified at the same time
* shortcoming : Timeliness , There may be cache inconsistencies
* scene : General timeliness , There are multiple services that need to be synchronized

<>2. be based on Canal Asynchronous notification for

* It is an open source project under Alibaba , be based on java development
* Incremental log parsing based on Database , Provide incremental data subscription and consumption
* be based on mysql The idea of master-slave backup
<>2.1 mysql Master-slave replication

<>2.2 canal working principle

* canal simulation MySQL slave Interactive protocol , Disguise yourself as MySQL slave , towards MySQL master send out dump agreement
* MySQL master received dump request , Start push binary log to slave ( Namely canal )
* canal analysis binary log object ( Original as byte flow )

Technology
©2019-2020 Toolsou All rights reserved,
evo Tool usage problems ——Degenerate covariance rank, Umeyama alignment is not possible Experiment 4 Automated test tools - software test mysql Export data sql sentence _mysql according to sql Query statement export data Create a thread ——— Javaweb (3) Data structure experiment ( three )—— Stacks and queues TS stay vue2 Writing in the project web Front end signature plug-in _signature_pad Plug in implements electronic signature function docker Where is the image stored Qt Getting Started tutorial 【 Basic controls 】QCalendarWidget calendar control springboot How to get reality in ip address