Now that I've built a general project structure, I'm starting to write some necessary tool classes. Today I'll start with the redis application that I use.Redis has many applications in some projects, so I'm going to talk about spring's String RedisTemplate today.The function redis uses in this project is to cache some information about WeChat.So you can think of it as a map collection.
Start with the API for String RedisTemplate:
stringRedisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS);//Store data in redis and set cache time
stringRedisTemplate.boundValueOps("test").increment(-1);//Valdo-1 operation
stringRedisTemplate.opsForValue().get("test")//Get val from cache based on key
stringRedisTemplate.boundValueOps("test").increment(1);//val +1
stringRedisTemplate.getExpire("test")//Get expiration time based on key
stringRedisTemplate.getExpire("test",TimeUnit.SECONDS)//Gets the expiration time based on the key and converts it to the specified unit
stringRedisTemplate.delete("test");//Delete cache based on key
stringRedisTemplate.hasKey("546545");//Check the existence of the key and return the boolean value
stringRedisTemplate.opsForSet().add("red_123", "1","2","3");//Store set collection in specified key
stringRedisTemplate.expire("red_123",1000 , TimeUnit.MILLISECONDS);//Set expiration time
stringRedisTemplate.opsForSet().isMember("red_123", "1")//View the existence of the specified data in the collection based on the key
stringRedisTemplate.opsForSet().members("red_123");//Get set collection from key
Let's move on to spring's redis configuration:
<!-- redis To configure -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<!-- <property name="password" value="${redis.pass}" /> -->
<property name="poolConfig" ref="poolConfig" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="enableTransactionSupport" value="false"/>
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<bean id="redisUtil" class="com.supcon.mo.util.RedisUtil">
<property name="redisTemplate" ref="redisTemplate" />
</bean>
This was discussed in the previous chapter when building the back-end architecture, and it is declared here that org.springframework.data.redis.core.StringRedisTemplate is spring's own redis template.
bean id="redisUtil" passes this template into the class com.supcon.mo.util.RedisUtil.But these are all things you need to do when spring starts.
Here is a case study of the methodology of a tool class:
public static RedisTemplate<String, Object> redisTemplate;
public static void setStringByTime(String key,Object obj, Long timeout, TimeUnit unit) {
if (obj == null) {
return;
}
if (timeout != null) {
redisTemplate.opsForValue().set(key, obj, timeout, unit);
} else {
redisTemplate.opsForValue().set(key, obj);
}
}
public RedisTemplate<String, Object> getRedisTemplate() {
return redisTemplate;
}
public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
Note here that a small mistake many people make is that when an IOC object injects an element, it must write a set, get method in the bean.
What I'm writing here is a key-value pair stored in redis that sets the expiration date for a certain period of time.
Timout writes numbers and unit writes units.For example, TimeUnit.SECONDS represents seconds.
Of course, this is not possible to write the main method test, you need to use junit to test.First Import
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-servlet-config.xml"})
These two comments, the IOC container that can be invoked.
Then test with @test before writing the method, such as:
@test
public void say(){
....A lot of test code, when main Method Write.
}
Since this tool class is registered in spring's configuration file, it's good to use the @Autowired tag injection when calling from other classes.
There is, of course, a question here, what if you want to reference it in other static tool classes?
Let me talk about the solution below:
Method 1:
@Autowired<br>//Notice that this is not static
private RedisUtil redisUtil;
private static LogUtil logUtil;
@PostConstruct //The @PostConstruct decorated method runs when the server loads Servle and is executed only once by the server.PostConstruct is executed after the constructor and before the init() method
public void init() {
logUtil = this;
logUtil.redisUtil = this.redisUtil;
}
public AdminLogService getUserService() {
return logService;
}
public void setUserService(rRdisUtil redisUtil) {
this.redisUtil= redisUtil;
}
The following call is made directly using the logUtil.redisUtil.method.
Method 2:
Remove the redisTemplate injection.Import the static template directly.
public static RedisTemplate<String, Object> redisTemplate;
To sum up, these are the applications of redis by individual projects, which have not been studied too deeply.For example, the principle of some redis, the advantages.Although I've seen it and I know it better, I don't think I'm talking professionally enough to mislead you.
Let's come on together!!!
What's wrong with it, or what's wrong with it? I keep it updated every morning on weekdays.