NoSQL
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自动配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- RedisAutoConfiguration 自动配置类;RedisProperties 属性类;spring.redis.xxx :对redis配置
- @Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class}):LettuceConnectionConfiguration.class 导入客户端类型 LettuceConnectionFactory 连接工厂也是配置好的;
JedisConnectionConfiguration.class 导入客户端类型 JedisConnectionFactory 连接工厂也是配置好的; - 容器自动注入 RedisTemplate<Object, Object>
- 容器自动注入 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
/**
* 测试连接整合redis
*/
@Test
void testRedis(){
ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
operations.set("hello","world");
String hello = operations.get("hello");
System.out.println("hello:"+hello);
}
1.3、切换至jedis
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
# 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;
/**
* 目标方法执行之前,每次requestURI就会计数加1
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String requestURI = request.getRequestURI();
//默认每次访问,当前requestURI就会计数加1
redisTemplate.opsForValue().increment(requestURI);
return true;
}
}
/**
* 定制一些spring的一些功能
* 1、编写一个拦截器实现WebMvcConfigurer接口
* 2、拦截器注册到容器中(实现addInterceptors的addInterceptor)
* 3、拦截所有,静态资源也会被拦截
* @EnableWebMvc 全面接管spring mvc , 慎用
* 1。静态资源、视图解析器、以及欢迎页等所以spring官方所列举的自动配置全部失效
*/
//@EnableWebMvc
@Configuration
public class AdminConfig implements WebMvcConfigurer {
/**
* Filter、Interceptor几乎有相同功能
* Filter是 Spring Servlet 定义的原生组件,好处:脱离Spring 应用也能使用
* Interceptor时Spring定义的接口,只能在Spring中用,可以使用Spring的自动装配等功能
*/
@Autowired
RedisUrlCountInterceptor redisUrlCountInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
//注册一个自定义拦截器
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/**") //拦截哪些请求,/** 表示所有请求,静态资源也会拦截
.excludePathPatterns("/","/login","/css/**","/fonts/**","/images/**","/js/**","/static/**","/sql","/acct","/city"); //放行请求
registry.addInterceptor(redisUrlCountInterceptor)
.addPathPatterns("/**") //拦截哪些请求,/** 表示所有请求,静态资源也会拦截
.excludePathPatterns("/","/login","/css/**","/fonts/**","/images/**","/js/**","/static/**","/sql","/acct","/city"); //放行请求
}
}
package com.atguigu.boot2.controller;
import com.atguigu.boot2.bean.Account;
import com.atguigu.boot2.bean.City;
import com.atguigu.boot2.bean.User;
import com.atguigu.boot2.service.AccountService;
import com.atguigu.boot2.service.CityService;
import com.atguigu.boot2.service.impl.AccountServiceImpl;
import com.atguigu.boot2.service.impl.CityServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
/**
* @Description:
* @ClassNmae: IndexController
* @Author: aowu
* @CreateDate: 2022/6/9 15:40
* @Version: 1.0
*/
@Slf4j
@Controller
public class IndexController {
@Autowired
StringRedisTemplate stringRedisTemplate;
/**
* 处理一个到 main.html 的请求
* @return
*/
@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<String, String> 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";
}
}
<div class="row state-overview">
<div class="col-md-6 col-xs-12 col-sm-6">
<div class="panel purple">
<div class="symbol">
<i class="fa fa-gavel"></i>
</div>
<div class="state-value">
<div class="value">[[${mainCount}]]</div>
<div class="title">/main.html</div>
</div>
</div>
</div>
<div class="col-md-6 col-xs-12 col-sm-6">
<div class="panel red">
<div class="symbol">
<i class="fa fa-tags"></i>
</div>
<div class="state-value">
<div class="value">[[${dynamicCount}]]</div>
<div class="title">/dynamicCount.html</div>
</div>
</div>
</div>
<div class="row state-overview">
<div class="col-md-6 col-xs-12 col-sm-6">
<div class="panel purple">
<div class="symbol">
<i class="fa fa-gavel"></i>
</div>
<div class="state-value">
<div class="value">[[${mainCount}]]</div>
<div class="title">/main.html</div>
</div>
</div>
</div>
<div class="col-md-6 col-xs-12 col-sm-6">
<div class="panel red">
<div class="symbol">
<i class="fa fa-tags"></i>
</div>
<div class="state-value">
<div class="value">[[${dynamicCount}]]</div>
<div class="title">/dynamicCount.html</div>
</div>
</div>
</div>