【参照资料】

1.spring boot 官网文档

2.https://www.cnblogs.com/gdpuzxs/p/7222309.html

【项目结构】

【pom.xml配置】

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

<groupId>lzf.test</groupId>
 <artifactId>demo.rediscache</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

<name>demo.rediscache</name>
 <description>Demo project for Spring Boot</description>

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.9.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>1.8</java.version>
 </properties>

<dependencies>
 
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-cache</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter</artifactId>
   <exclusions>
    <exclusion>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-logging</artifactId>
    </exclusion>
   </exclusions>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-log4j2</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
 </dependencies>

<build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>

</project>

【application.properties配置】

  

spring.cache.type = redis
spring.cache.cache-names=cache1
#spring.cache.redis.
spring.redis.database=0 
# Redis服务器地址
spring.redis.host=192.168.6.88
# Redis服务器连接端口
spring.redis.port=6379 
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8 
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1 
# 连接池中的最大空闲连接
spring.redis..pool.max-idle=8 
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0 
# 连接超时时间(毫秒)
spring.redis.timeout=0
spring.cache.redis.time-to-live=600000

## LOG4J配置
log4j.rootCategory=DEBUG,stdout
## 控制台输出
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n

【相关java文件】

  【Application.java】

     

package lzf.test.demo.rediscache;

import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import lzf.test.demo.rediscache.entity.User;
import lzf.test.demo.rediscache.service.OperateUser;

@SpringBootApplication
@RestController
@EnableScheduling
@EnableCaching
public class Application {

public static void main(String[] args) {
  SpringApplication.run(Application.class, args);
 }
 
 
 @Autowired
 private RedisCacheManager redisCacheManager;
 @Autowired
 private StringRedisTemplate stringRedisTemplate;
 @Autowired
 private OperateUser operateUser;
 @Autowired
 private RedisTemplate redisTemplate;
 
 @RequestMapping("/age/{age}")
 public String testRedis(@PathVariable String age){
  ValueOperations<String, String> ops =stringRedisTemplate.opsForValue();
  
  ops.set("bbb", age);
  RedisConnection connection = stringRedisTemplate.getConnectionFactory().getConnection();
  List<String> config = connection.getConfig("hostname");
  //--------------------------
  
  Collection<String> cacheNames = redisCacheManager.getCacheNames();
  Cache cache = null;
  for(String s : cacheNames){
   cache = redisCacheManager.getCache(s);
   
  }
  return age; 
  
 }
 /**
  * TODO 查询缓存
  * @author 刘宗峰
  * @param username
  * @return
  */
 @RequestMapping("/user/{username}")
 @ResponseBody
 public List<User> findUsers(@PathVariable String username){
  ValueOperations opsForValue = redisTemplate.opsForValue();
  Set keys = redisTemplate.keys("*wang5*".getBytes());
  for(Object key : keys){
   System.out.println(key.toString());
  }
  List<User> us = operateUser.findUser(username);
  return us;
  
 }
 /**
  * TODO 删除时清理缓存
  * @author 刘宗峰
  * @param username
  * @return
  */
 @RequestMapping("/del/{username}")
 public String delUser(@PathVariable String username){
  
  operateUser.delUser(username);
  return "success";
 }
 
 @RequestMapping("/update/{username}/{age}")
 public String updateUser(@PathVariable String username,@PathVariable Integer age){
  operateUser.updateUser(username, age);
  ValueOperations opsForValue = redisTemplate.opsForValue();
  Set keys = opsForValue.getOperations().keys("*wang5*");
  for(Object key : keys){
   System.out.println(key.toString());
  }
  System.out.println(opsForValue.get(username));
  return "success";
 }
}

   【OperateUser.java】

    

package lzf.test.demo.rediscache.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;

import lzf.test.demo.rediscache.entity.User;
@Service
@CacheConfig(cacheNames = "cache1")
public class OperateUser {

//1.查询user @Cacheable将查询结果缓存到redis中,(key="#p0")指定传入的第一个参数作为redis的key
 @Cacheable(key ="#p0")
 public List<User> findUser(@Param("username") String username){
  
  List<User> us = new ArrayList<User>();
  User u1 = new User("wang5",12);
  //User u2 = new User("zhao6",15);
  
  us.add(u1);
  //us.add(u2);
  
  return us;
 }
 //如果指定为 true,则方法调用后将立即清空所有缓存
 @CacheEvict(key ="#p0",allEntries=true)
 public void delUser(@Param("username") String username){
  //
 }
 @CachePut(key = "#p0")
 public List<User> updateUser(@Param("username") String username,Integer age){
  List<User> us = new ArrayList<User>();
  User u1 = new User(username,age);
  us.add(u1);
  return us;
 }
}

---------------------------------------------------------------------------

缓存使用的没问题。但是想要手动操作数据,但是没能取出key--value

springboot + redis缓存使用的更多相关文章

  1. springboot redis 缓存对象

    只要加入spring-boot-starter-data-redis , springboot 会自动识别并使用redis作为缓存容器,使用方式如下 gradle加入依赖 compile(" ...

  2. SpringBoot Redis缓存 @Cacheable、@CacheEvict、@CachePut

    文章来源 https://blog.csdn.net/u010588262/article/details/81003493 1. pom.xml <dependency> <gro ...

  3. springboot Redis 缓存

    1,先整合 redis 和 mybatis 步骤一: springboot 整合 redis 步骤二: springboot 整合 mybatis 2,启动类添加 @EnableCaching 注解, ...

  4. 从.Net到Java学习第七篇——SpringBoot Redis 缓存穿透

    从.Net到Java学习系列目录 场景描述:我们在项目中使用缓存通常都是先检查缓存中是否存在,如果存在直接返回缓存内容,如果不存在就直接查询数据库然后再缓存查询结果返回.这个时候如果我们查询的某一个数 ...

  5. 微服务-Springboot+Redis缓存管理接口代码实现

    废话少说,上代码,结合代码讲解: 一.创建maven工程:导入依赖: <packaging>war</packaging><!--修改jdk的版本--><pr ...

  6. springboot redis 缓存跨域丢失问题

    对于前后端分离的项目,在开发阶段经常会遇到跨域的问题. 1.首先,对于后台的处理方式,第一种是用 @CrossOrigin 注解,@crossorigin是后端SpringMVC框架(需4.2版本以上 ...

  7. spring-boot的spring-cache中的扩展redis缓存的ttl和key名

    原文地址:spring-boot的spring-cache中的扩展redis缓存的ttl和key名 前提 spring-cache大家都用过,其中使用redis-cache大家也用过,至于如何使用怎么 ...

  8. SpringBoot学习笔记(6) SpringBoot数据缓存Cache [Guava和Redis实现]

    https://blog.csdn.net/a67474506/article/details/52608855 Spring定义了org.springframework.cache.CacheMan ...

  9. java框架之SpringBoot(11)-缓存抽象及整合Redis

    Spring缓存抽象 介绍 Spring 从 3.1 版本开始定义了 org.springframework.cache.Cache 和 org.springframework.cache.Cache ...

随机推荐

  1. php中常用的字符串格式化函数

    ltrim():从字符串左删除空格或其他预定义字符串 rtrim():从字符串的末端开始删除空白字符串或其它预定义字符 trim():从字符串的两端删除空白字符和其他预定字符 str_pad():把字 ...

  2. 接口测试思路,jmeter,接口测试流程

    接口测试总结 一:接口测试思想 接口测试:通过向服务器端发送请求,获取响应与预期结果做对比的一种服务端黑盒测试过程. 解释:接口就是将浏览器,客户端,手机端,或者服务器调用另一个服务器的请求抽离出来测 ...

  3. [SVN服务器搭建] 在myecplise下使用的 tortoise1.9 64位 跟 subversion1.9的服务器使用

    由于在公司经常用到SVN服务器,所以自己也想搭建在本机上面搭建一个SVN服务器玩玩,废话不多说,下面直接贴出来如何搭建的.  一.tortoise1.9 64位下载 直接百度下载即可,百度时候需要显示 ...

  4. Vue 国际化 vue-i18n 用法详解

    vue-i18n 仓库地址:https://github.com/kazupon/vue-i18n 兼容性: 支持 Vue.js 2.x 以上版本 安装方法:(此处只演示 npm) npm insta ...

  5. Hibernate 一对一双向映射 注解方式

    有外键的一方: @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "courseid") public Tcourse ge ...

  6. Yii2之行为

    Yii三大特性:属性.事件.行为.前面两篇文章已经分别讲解了属性和事件,本文接着讲讲yii的行为,分析yii行为的实现原理. 在yii中,一个对象绑定了行为之后,就拥有了所绑定行为拥有的所有事件,而且 ...

  7. LeetCode 538. Convert BST to Greater Tree (把二叉搜索树转换成较大树)

    Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...

  8. makefile学习笔记(一)

    1.1:make概述 在linux环境下使用make工具能够比较容易的构建一个属于自己的工程,整个工程的编译只需要一个命令就可以完成编译.连接以至于最后的执行.不过我们需要投入一些时间去学习如何完成m ...

  9. Problem E 

    Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计 ...

  10. Strange fuction

    Strange fuction Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...