redis是一种可基于内存也可基于持久话的日志型、key-value数据库。因为性能高,存储数据类型丰富等优势常被用作数据缓存。

我们利用spring-boot-autoconfiguration.jar包中已有的RedisAutoConfiguration.class类来获取RedisTemplate对象和StringRedisTemplate对象,这样我们只需要在application.yml中增加相应的参数就可以了。

pom.xml :

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>1.5.9.RELEASE</version>
</dependency>

application.yml :

spring:
redis:
# 数据库索引
database: 0
host: 192.168.100.100
port: 6379
# 连接超时时间(毫秒)
timeout: 30000
pool:
# 连接池最大连接数
max-active: 2000
# 连接池最大阻塞等待时间
max-wait: 6000
# 连接池中的最大空闲连接
max-idle: 300
# 连接池中的最小空闲连接
min-idle: 100

Application:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class Application {
private static final Logger LOG = LoggerFactory.getLogger(Application.class); public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.setBannerMode(Banner.Mode.OFF);
app.setWebEnvironment(true);
app.run(args);
LOG.info("**************** Startup Success ****************");
}
}

RedisController:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class RedisController { @Autowired
RedisUtils redisUtils; @RequestMapping("/redis")
public String login() {
redisUtils.set("hello", "world");
String string = redisUtils.get("hello");
return string;
} }

RedisUtils:

import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component; @Component
public class RedisUtils { @Autowired
private StringRedisTemplate stringRedisTemplate; /**
*
* 写入缓存<br/>
* @param key
* @param value
* @return
*/
public boolean set(String key, String value) {
try {
stringRedisTemplate.opsForValue().set(key, value); return true;
} catch (Exception e) {
e.printStackTrace(); return false;
}
} /**
*
* 写入缓存,设置过期时间<br/>
* @param key
* @param value
* @param time
* @param timeUnit
* @return
*/
public boolean set(String key, String value, long time, TimeUnit timeUnit) {
try {
if (time > 0) {
stringRedisTemplate.opsForValue().set(key, value, time, timeUnit);
} else {
set(key, value);
} return true;
} catch (Exception e) {
e.printStackTrace(); return false;
}
} /**
*
* 获取缓存数据<br/>
* @param key
* @return
*/
public String get(String key) {
return key == null ? "" : stringRedisTemplate.opsForValue().get(key);
} /**
*
* 判断key是否存在<br/>
* @param key
* @return
*/
public boolean exists(String key){
boolean result = false;
try {
result = stringRedisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
}
return result;
} /**
*
* 移除key<br/>
* @param key
* @return
*/
public boolean remove(String key){
boolean result = false;
try {
if(exists(key)){
stringRedisTemplate.delete(key);
}
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
} }

springboot1.5.9 整合单机版redis3.2.8的更多相关文章

  1. 基于springboot1.5.9整合shiro时出现静态文件找不到的问题

    开门见山吧,上午对shiro进行整合了下,因为之前使用ssm框架对shiro框架整合过,所以觉得使用springboot再次对shiro框架进行整合也是没啥问题,但最后整合完之后,使用thymelea ...

  2. springboot1.5.9整合websocket实现实时显示的小demo

    最近由于项目需要实时显示数据库更新的数据变化情况,一开始想过在前端使用ajax异步轮询方法实现,但后面考虑到性能和流量等要求,就放弃该方法而选择使用websocket(毕竟现在springboot整合 ...

  3. Springboot1.5.9整合WebSocket

    一.WebSocket介绍 1.WebSocket是什么? WebSocket是协议,是HTML5开始提供的基于TCP(传输层)的一种新的网络协议, 它实现了浏览器与服务器全双工(full-duple ...

  4. 【SpringBoot】Springboot1.5.9整合WebSocket

    一.WebSocket介绍 1.WebSocket是什么? WebSocket是协议,是HTML5开始提供的基于TCP(传输层)的一种新的网络协议, 它实现了浏览器与服务器全双工(full-duple ...

  5. SpringBoot之整合Redis

    一.SpringBoot整合单机版Redis 1.在pom.xml文件中加入redis的依赖 <dependency> <groupId>org.springframework ...

  6. spring 整合 redis 单机版

    增加spring配置文件: application-jedis.xml <?xml version="1.0" encoding="UTF-8"?> ...

  7. spring整合dubbo[单机版]

    Spring整合Dubbo,这个是用xml配置的 (方式一) 来梳理下步骤: 1. 安装zookeeper,在进行简单配置[这里使用单机模式,不用集群] 2. 创建maven项目,构建项目结构 3. ...

  8. Spring boot 整合redis单机版

       一.安装redis 这个不多说,网上有各种系统安装redis的操作, redis安装 二.创建sprigboot项目 这个也不多说,不会的前面有相关教程. 三.添加maven坐标 四.编写spr ...

  9. Docker整合dockerfly实现UI界面管理(单机版)

    一.搜索镜像 docker search dockerfly 二.根据镜像使用排名(一般情况下拉取使用率最高的镜像名),我这里使用的是阿里云镜像地址 docker pull registry.cn-h ...

随机推荐

  1. Luogu1287 | 盒子与球 (排列组合)

    贴一个和其他题解不一样的做法 QWQ 题意:让我们求出 N 个球放入 R 个盒子且每个盒子都必须放球方案数. 首先,对于每一个球,可以将其放入的盒子数量共有 R 个,所以我们可以知道如果无需满足每个盒 ...

  2. Leetcode Week4 Find Minimum in Rotated Sorted Array II

    Question Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforeha ...

  3. vjudge 棋盘

    原题目链接:https://vjudge.net/contest/331118#problem/B 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放 ...

  4. 启动Hive时报错(com.mysql.jdbc.Driver") was not found in the CLASSPATH)

    这是因为没有mysql-connector的jar包.需要把jar包复制到hive目录lib文件夹中. 参考博客:https://blog.csdn.net/Realoyou/article/deta ...

  5. Python之tcp server模拟Http通信

    1.python tcp server代码: import socket def main(): tcp_server_socket = socket.socket(socket.AF_INET, s ...

  6. 栈的简单应用之中缀表达式转后缀表达式(C语言实现逆波兰式)

    一.前言   普通人在书写计算式时会选择中缀表达式,这样符合人脑的认知习惯.可计算机处理时后缀表达式才能使处理速度更快,其原因是利用堆栈结构减少计算机内存访问.同时它也是一个很好锻炼栈这个数据结构的应 ...

  7. Deepin安装常用软件

    Deepin安装常用软件 安装git sudo apt-get install git sudo是Debian系列以管理员运行的前缀 卸载软件 sudo apt-get remove package_ ...

  8. 《TCP/IP入门经典》摘录--Part 3

    TCP/IP协议系统 3.子网划分和CIDR 子网 划分网络 为什么需要划分子网? 子网划分就是在网络 ID 之下提供了第 2 层逻辑组织.路由器能够把数据报发送给网络里的某个子网地址(一般对应于一个 ...

  9. IIS新类型文件500错误

    时间过得太久,就真的会忘掉.在这里记录一下吧. 不得已重新安装系统,然后以前的配置都忘掉了.对新类型的文件.geojson 文件报错. 500错误. 首先反复调试MIME类型是否有问题, 再看映射是否 ...

  10. ORA-01935: missing user or role name

    问题描述 ORA-01935: missing user or role name ORA-01935:缺少用户或角色名