前言,本篇博客將具體描述如何使用在springboot中引入redis作爲緩存使用
一、相關環境
spingboot 2.0
redis
二、代碼相關內容
1、添加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.38</version>
</dependency>
2、application.propertities添加一些配置
spring.redis.host=127.0.0.0
spring.redis.port=6379
3、添加RedisConfig.java
package com.example.config;
import com.example.Util.FastJsonSerializerForRedis;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public LettuceConnectionFactory lettuceConnectionFactory(){
LettuceConnectionFactory lettuceConnectionFactory=new LettuceConnectionFactory();
return lettuceConnectionFactory;
}
@Bean(name = "myRedisTemplate")
public RedisTemplate functionDomainRedisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new FastJsonSerializerForRedis<>(Object.class));
redisTemplate.setValueSerializer(new FastJsonSerializerForRedis<>(Object.class));
// 開啓事務
redisTemplate.setEnableTransactionSupport(true);
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
return redisTemplate;
}
}
4、自定義序列化(重要)
redis裏的數據都按json格式傳輸,如果不設置redis的序列化,redis裏存入的數據就都是亂碼,沒辦法在redis中直接查看數據
package com.example.Util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import java.nio.charset.Charset;
public class FastJsonSerializerForRedis<T> implements RedisSerializer<T> {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private Class<T> clazz;
public FastJsonSerializerForRedis(Class<T> clazz) {
super();
this.clazz = clazz;
}
@Override
public byte[] serialize(T t) throws SerializationException {
if (t == null) {
return new byte[0];
}
return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
}
@Override
public T deserialize(byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length <= 0) {
return null;
}
String str = new String(bytes, DEFAULT_CHARSET);
return (T) JSON.parseObject(str, clazz);
}
}
5、添加redis操作工具類
@Component
public class RedisUtil {
@Resource(name = "myRedisTemplate")
private RedisTemplate redisTemplate;
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
}
代碼比較多,就不都放上來了,源碼在我github上有
6、添加redis結果
三、redis安裝的一些步驟
由於這一部分並不是所有人都需要就放到最後面了
1、下載redis
redis的github路徑:https://github.com/MicrosoftArchive/redis/releases
下載之後解壓,在文件夾下,雙擊redis-server.exe就可以啓動。(以.conf結尾的文件是redis的配置文件,可以修改包括redis的端口、密碼、備份等一些參數,需要修改的可以去百度一下)
雙擊之後
如果想要隨開機啓動添加到windows的服務裏面即可,這裏不展開來講
2、redis的圖形化管理工具
使用過幾款,比較喜歡的還是RedisDesktopManager
下載路徑:https://github.com/uglide/RedisDesktopManager/releases
項目源碼鏈接:https://github.com/BryceHuang/springboot-learn.git