Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 与范围查询, bitmaps, hyperloglogs 和 地理空间(geospatial) 索引半径查询。 Redis 内置了 复制(replication),LUA脚本(Lua scripting), LRU驱动事件(LRU eviction),事务(transactions) 和不同级别的 磁盘持久化(persistence), 并通过 Redis哨兵(Sentinel)和自动 分区(Cluster)提供高可用性(high availability)。
1.1、Redis自动配置org.springframework.boot spring-boot-starter-data-redis
- RedisAutoConfiguration 自动配置类;RedisProperties 属性类;spring.redis.xxx :对redis配置
- @Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class}):LettuceConnectionConfiguration.class 导入客户端类型 LettuceConnectionFactory 连接工厂也是配置好的;
JedisConnectionConfiguration.class 导入客户端类型 JedisConnectionFactory 连接工厂也是配置好的; - 容器自动注入 RedisTemplate
- 容器自动注入 StringRedisTemplate,key:value,二者都是 String类型
- 允许key:value 二者都是object类型
- 在这底层中只有我们使用StringRedisTemplate、RedisTemplate就可以操作redis
redis环境搭建
1、阿里云按量付费redis。经典网络
2、申请redis的公网连接地址
3、白名单设置,127.0.0.1仅仅本机访问,修改白名单允许0.0.0.0/0 允许所有地址访问
1.2、RedisTemplate与Lettuce
@Test
void testRedis(){
ValueOperations operations = stringRedisTemplate.opsForValue();
operations.set("hello","world");
String hello = operations.get("hello");
System.out.println("hello:"+hello);
}
1.3、切换至jedis
org.springframework.boot spring-boot-starter-data-redis redis.clients jedis
# redis配置
spring:
redis:
host: r-7xvnzvfphwycfwzzi7pd.redis.rds.aliyuncs.com #公网地址
port: 6379 #端口号
password: admin:admin #账号:密码
client-type: jedis #切换客户机为 jedis 或者 lettuce
jedis:
pool:
max-active: 10 #最大活动数
min-idle: 5 #最小空闲
实例:
@Component
public class RedisUrlCountInterceptor implements HandlerInterceptor {
@Autowired
StringRedisTemplate redisTemplate;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String requestURI = request.getRequestURI();
//默认每次访问,当前requestURI就会计数加1
redisTemplate.opsForValue().increment(requestURI);
return true;
}
}
//@EnableWebMvc
@Configuration
public class AdminConfig implements WebMvcConfigurer {
@Autowired
RedisUrlCountInterceptor redisUrlCountInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
//注册一个自定义拦截器
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("
@Slf4j
@Controller
public class IndexController {
@Autowired
StringRedisTemplate stringRedisTemplate;
@GetMapping({"/main.html"})
public String mainPage(HttpSession session,Model model){
log.info("当前方法是:"+"mainPage");
// 是否登录,使用拦截器、过滤器,若是做了拦截器,可以注释掉
// Object loginUser = session.getAttribute("loginUser");
// if (loginUser != null) {
// return "main";
// }else {
// model.addAttribute("msg","请重新登录");
// //回到登录页
// return "login";
// }
ValueOperations opsForValue = stringRedisTemplate.opsForValue();
String s = opsForValue.get("/main.html");
String s1 = opsForValue.get("/dynamic_table");
model.addAttribute("mainCount",s);
model.addAttribute("dynamicCount",s1);
return "main";
}
}
[[${mainCount}]]/main.html[[${dynamicCount}]]/dynamicCount.html[[${mainCount}]]/main.html[[${dynamicCount}]]/dynamicCount.html



