Used in the company's projectsRedis, I've been learning about this recently. From the beginning to the present, The general feeling for me isRedis It's easier to get started, But mastery requires a lot of practice and research. Fortunately, our company is rightRedis Its use is not so deep, At present, only some simple operations can be carried out. Summarize the learning results of these days, Record, Hope helps consolidate and improve.

  Explain: MyRedis Service is built on virtual machineLinux(Read
Heat6.2) On the system,Mysql Database is installed on this machine, The network connection mode of virtual machine is bridging, Share a network segment with this computer. There are many examples of building on the Internet, I won't explain it here.

  First isSpringBoot Profile in(application.yml Profile of type, This type of document has strict requirements for writing format)

server: port: 8090 spring: datasource: url:
username:xxx password: xxx driver-class-name: com.mysql.jdbc.Driver
test-on-borrow:false test-while-idel: true time-between-eviction-runs-millis:
3600000tomcat: max-wait: 10000 max-active: 50 http: encoding: force: true
jpa: hibernate: ddl-auto: update max_fetch_depth: 2 naming: physical-strategy:
hibernate: dialect: org.hibernate.dialect.MySQL5Dialect max_fetch_depth: 2
format_sql:true show-sql: true redis: host: xxx password: port: 6379 pool: #
Maximum free connections in connection pool max-idle: 8 min-idle: 0 # Connection pool maximum connections( Use a negative value to indicate no limit) max-active: 8 #
Connection pool maximum block wait time( Use a negative value to indicate no limit) max-wait: 1 # Connection timeout( Millisecond) timeout: 60000

The next thing is rightRedis Configuration of data operation class, Provides a way to serialize data.Spring EncapsulatedRedisTimplement Object to rightRedis Operation, The template defaults toJdkSerializationRedisSerializer

Binary serialization of.

import com.fasterxml.jackson.annotation.JsonAutoDetect; import
org.springframework.beans.factory.annotation.Autowired; import
org.springframework.context.annotation.Bean; import
org.springframework.context.annotation.Configuration; import;import;import;import;import;import;@Configuration
// Annotate a configuration class, Hand over tospring Administration public class Config extends CachingConfigurerSupport{ /* *
adoptRedisConnectionFactory Introduceredis Connection configuration in configuration file * */ @Autowired private
RedisConnectionFactoryfactory; /* * 1. Why serialization here * Reason: There is no way to use ordinary connectionJava Object direct depositRedis
, * We need to provide our own solutions- Object serialization , Then depositredis, * After retrieving serialized content, Convert tojava object * */ @Bean public
RedisTemplate<String,Object> redisTemplate(){ RedisTemplate<String,Object>
template =new RedisTemplate<>(); initDomainRedisTemplate(template,factory);
returntemplate; } public void initDomainRedisTemplate(RedisTemplate
template,RedisConnectionFactory factory){
template.setConnectionFactory(factory);// UseJackson2JsonRedisSerializer To serialize and deserialize
redis Of value value Jackson2JsonRedisSerializer<Object> serializer = new
Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper mapper = new
ObjectMapper();//ObjectMapper yesjackson Main classes, Action serialization
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); //
Set any field visible mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); //
De serialization serializer.setObjectMapper(mapper);
template.setValueSerializer(serializer); // Set serialization and deserializationredis Ofvalue value
template.setKeySerializer(new StringRedisSerializer()); // Set serialization deserializationkey The value is
String template.afterPropertiesSet(); // Initialize operation, After setting } // instantiationHashOperations
object, have access toHash Type operation @Bean public HashOperations<String,String,Object>
hashOperations(RedisTemplate<String,Object> redisTemplate){return
redisTemplate.opsForHash();//opsForHash() This method is used for operationHash } // instantiationListOperations
object, have access toList Type operation @Bean public ListOperations<String,Object>
listOperations(RedisTemplate<String,Object> redisTemplate){return
redisTemplate.opsForList();//opsForList() This method is used for operationList } }
And then it's rightRedis Tool class for data operation, Three types of data are listed here,String,Hash,List import
org.springframework.beans.factory.annotation.Autowired; import;import;import;import
org.springframework.stereotype.Component; import java.util.List; import
java.util.Map;import java.util.concurrent.TimeUnit; @Component public class
RedisUtil { /* * Explain: Personal summary:redisTemplate Can handleredis All data types in. *
But for the processing of specific data types, All kinds of interfaces are easier to handle, * There are many methods * */ @Autowired private
RedisTemplate<String,Object>redisTemplate; @Autowired private
HashOperations<String,String,Object>hashOperations; @Autowired private
/*--------------------------------------------- Public area
---------------------------------------------------*/ // judgekey Does it exist? public
booleanhasKey(String key){ return redisTemplate.hasKey(key); } // according tokey deletekey and
value public void deleteByKey(String key){ redisTemplate.delete(key); } /* * Set up
key Validity period * @param key,time( Effective time),unit(hours,days,minutes) * */ public void
setExpire(String key,long time,TimeUnit unit){ if (time != -1){ redisTemplate
.expire(key,time,unit); } }/*--------------------------------------------- Public area
// according tokey Obtainvalue value public String getValue(String key){ return (String)
redisTemplate.opsForValue().get(key); } // insert data public void setValue(String
key,String value){redisTemplate.opsForValue().set(key,value); }

// Deposit inhash Type value public void addAllForHash(String key, Map<String,Object> map){
hashOperations.putAll(key,map); } /* * according tokey Query thekey Correspondingmap All undervalue * @param key
* @return List<AllValue> * */ public List<Object> getAllValueForHash(String
key){return hashOperations.values(key); } // ObtainHash Single invalue public Object
getValueForHash(String key, String hashKey){return hashOperations
.get(key,hashKey); }// ObtainHash Number public Long getCount(String key){ return
hashOperations.size(key); } /* * according tokey andhashKey deletevalue * @param key,hashKey *
@return void * */ public void deleteValueByKeyAndHashKey(String key,String
hashKey){hashOperations.delete(key,hashKey); } /* * Current judgementkey inhashKey Does it exist? *
@param key,hashKey * @return true / false * */ public boolean hasHashKey(String
key,String hashKey){return hashOperations.hasKey(key,hashKey); } /* * querykey All under
Hash value * @param key * @return Map<key,value> * */ public Object
getAllHashByKey(String key){return hashOperations.entries(key); }

/* * fromlist Queue tail data * @param key,List * @return null * */ public void
rightPushAll(String key, List<String> values){listOperations
.rightPushAll(key,values); } /* * fromlist Queue tail data * @param key,value * @return
null * */ public void rightPush(String key, String value){ listOperations
.rightPush(key,value); }/* * fromlist Get all data in the queue(0,listSize) * @param key * @return
List * */ public List<Object> getListAll(String key){ return listOperations
.range(key,0,listOperations.size(key)); }