环境
  eclipse 4.7
  jdk 1.8
  Spring Boot 1.5.2

一、Spring Boot Cache以及整合EhCache
Spring从3.1开始定义了org.springframework.cache.Cache和org.springframework.cache.CacheManager接口来统一不同的缓存技术;并支持使用JCache(JSR-107)注解简化我们开发;
  Cache接口为缓存的组件规范定义,包含缓存的各种操作集合;
  Cache接口下Spring提供了各种xxxCache的实现;如RedisCache,EhCacheCache ,ConcurrentMapCache等;
  每次调用需要缓存功能的方法时,Spring会检查指定参数的指定的目标方法是否已经被调用过;如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法并缓存结果后返回给用户。下次调用直接从缓存中获取。

详细参考:Spring Boot Cache使用与整合

二、整合Redis

1、引入依赖

<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>com.wjy</groupId>
<artifactId>springboot-redis</artifactId>
<version>0.0.1-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<finalName>${project.artifactId}</finalName>
</build> </project>

2、application.properties配置redis

# Redis数据库索引(默认为0)
spring.redis.database=1
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=123456
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=1000
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=10
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=2
# 连接超时时间(毫秒)
spring.redis.timeout=0

3、service

/**
*
*/
package com.wjy.service; import java.util.ArrayList;
import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; @Service
public class RedisService { @Autowired
private StringRedisTemplate stringRedisTemplate;//操作key-value都是字符串 @Autowired
private RedisTemplate redisTemplate;//操作key-value都是对象 /**
* Redis常见的五大数据类型:
* stringRedisTemplate.opsForValue();[String(字符串)]
* stringRedisTemplate.opsForList();[List(列表)]
* stringRedisTemplate.opsForSet();[Set(集合)]
* stringRedisTemplate.opsForHash();[Hash(散列)]
* stringRedisTemplate.opsForZSet();[ZSet(有序集合)]
*/
public void setValue(String key,Object value){
if (value instanceof String)
{
stringRedisTemplate.opsForValue().set(key,value.toString());
}
else if (value instanceof List)
{
List<String> list = (ArrayList<String>)value;
for (String s : list)
{
stringRedisTemplate.opsForList().leftPush(key, s);
}
}
} public String getStringKey(String key) {
return stringRedisTemplate.opsForValue().get(key);
} }

4、controller

/**
*
*/
package com.wjy.controller; import java.util.ArrayList;
import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.wjy.service.RedisService; @RestController
public class IndexController { @Autowired
private RedisService redisService; @RequestMapping("/setString")
public String setString(String key,String value) {
redisService.setValue(key, value);
return "success";
} @RequestMapping("/setList")
public String setList(String key) {
List<String> list = new ArrayList<String>();
list.add("123");
list.add("456");
redisService.setValue(key, list);
return "success";
} @RequestMapping("/getKey")
public String getKey(String key) { return "success";
} }

5、App

package com.wjy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class App { public static void main(String[] args) {
SpringApplication.run(App.class, args);
} }

扩展:在redis中如何存储对象?

使用String类型,将对象转换成json字符串存储,获取对象时,将json串反序列化为对象。

【Spring Boot学习之九】缓存支持的更多相关文章

  1. spring boot学习(十三)SpringBoot缓存(EhCache 2.x 篇)

    SpringBoot 缓存(EhCache 2.x 篇) SpringBoot 缓存 在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManag ...

  2. spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战

    SpringBoot + Redis +SpringSession 缓存之实战 前言 前几天,从师兄那儿了解到EhCache是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...

  3. Spring Boot 学习笔记(六) 整合 RESTful 参数传递

    Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...

  4. Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客

    ==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...

  5. Github点赞超多的Spring Boot学习教程+实战项目推荐!

    Github点赞接近 100k 的Spring Boot学习教程+实战项目推荐!   很明显的一个现象,除了一些老项目,现在 Java 后端项目基本都是基于 Spring Boot 进行开发,毕竟它这 ...

  6. Spring Boot 整合 Redis 实现缓存操作

    摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢!   『 产品没有价值,开发团队再优秀也无济于事 – <启示录> 』   本文提纲 ...

  7. Spring Boot 学习(1)

    文 by / 林本托 Tip 做一个终身学习的人. Spring Boot 初体验 Spring Boot 包含了很多 start(Spring boot 中 的叫法,就是一个模块,后文统一称模块,便 ...

  8. Spring Boot + Mybatis + Redis二级缓存开发指南

    Spring Boot + Mybatis + Redis二级缓存开发指南 背景 Spring-Boot因其提供了各种开箱即用的插件,使得它成为了当今最为主流的Java Web开发框架之一.Mybat ...

  9. Spring boot学习1 构建微服务:Spring boot 入门篇

    Spring boot学习1 构建微服务:Spring boot 入门篇 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...

随机推荐

  1. 关键字explicit的作用(转)

    C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用是表明该构造函数是显示的, 而非隐式的, 跟它相对应的另一个关键字是implicit, 意思是隐藏的,类构造函数默认情况 ...

  2. (7)树莓派读物USB摄像头

    https://blog.csdn.net/qq_42403190/article/details/90453305 创建文件 camera.py 简单读取 #!/usr/bin/env python ...

  3. JavaScript原始类型转换和进制转换

    1.JavaScript转换包括:强制转换和基本转换 如: var  str = 'A',num=10,nu=null,t=true,und=undefined,x; //注意:定义的x未被初始化:默 ...

  4. cf1189解题报告

    cf1189div2解题报告 codeforces A 答案要不是一串要不就是去掉最后一个字母的两串 #include <bits/stdc++.h> #define ll long lo ...

  5. Cogs 513. 八(容斥原理)

    八 ★☆ 输入文件:eight.in 输出文件:eight.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 八是个很有趣的数字啊.八=发,八八=爸爸,88=拜拜.当然最有趣的 ...

  6. “知乎杯”2018 CCF 大学生计算机系统与程序设计竞赛 分组加密器(encryption)

    分组加密器(encryption) 题解点这里 #include<map> #include<stack> #include<vector> #include< ...

  7. UDF——已知入口压力和流量计算压降

    有时候我们在计算内流,比如管道内的流动时,只知道入口压力和流量,而我们想要计算得到出口的压力,这个应该怎么办呢?当然新版本的Fluent已经自带了流量出口边界,而这里我们采用Fluent的UDF来实现 ...

  8. 集合类 Map接口 HashTable

    集合类的另外一种重要实现为Map接口,Map接口提供的方法如下: Map接口一个不常见实现为HashTable,HashTable对所有有并发访问问题的方法通过 synchronized 关键字进行并 ...

  9. Java的string类为什么是不可变的

    最流行的Java面试题之一就是:什么是不可变对象(immutable object),不可变对象有什么好处,在什么情况下应该用,或者更具体一些,Java的String类为什么要设成immutable类 ...

  10. Asp.Net Core Cookie使用,Asp.net Core Cookie操作失效

    注:本文主要介绍Asp.net Core 3.0后增加cookie代理功能. 默认是增加了的. 默认增加的这个有些问题所在, 1.原来设置cookie方式将不可用,需要按照代理方式设置 2.对于ses ...