springboot1.5.9 整合单机版redis3.2.8
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的更多相关文章
- 基于springboot1.5.9整合shiro时出现静态文件找不到的问题
开门见山吧,上午对shiro进行整合了下,因为之前使用ssm框架对shiro框架整合过,所以觉得使用springboot再次对shiro框架进行整合也是没啥问题,但最后整合完之后,使用thymelea ...
- springboot1.5.9整合websocket实现实时显示的小demo
最近由于项目需要实时显示数据库更新的数据变化情况,一开始想过在前端使用ajax异步轮询方法实现,但后面考虑到性能和流量等要求,就放弃该方法而选择使用websocket(毕竟现在springboot整合 ...
- Springboot1.5.9整合WebSocket
一.WebSocket介绍 1.WebSocket是什么? WebSocket是协议,是HTML5开始提供的基于TCP(传输层)的一种新的网络协议, 它实现了浏览器与服务器全双工(full-duple ...
- 【SpringBoot】Springboot1.5.9整合WebSocket
一.WebSocket介绍 1.WebSocket是什么? WebSocket是协议,是HTML5开始提供的基于TCP(传输层)的一种新的网络协议, 它实现了浏览器与服务器全双工(full-duple ...
- SpringBoot之整合Redis
一.SpringBoot整合单机版Redis 1.在pom.xml文件中加入redis的依赖 <dependency> <groupId>org.springframework ...
- spring 整合 redis 单机版
增加spring配置文件: application-jedis.xml <?xml version="1.0" encoding="UTF-8"?> ...
- spring整合dubbo[单机版]
Spring整合Dubbo,这个是用xml配置的 (方式一) 来梳理下步骤: 1. 安装zookeeper,在进行简单配置[这里使用单机模式,不用集群] 2. 创建maven项目,构建项目结构 3. ...
- Spring boot 整合redis单机版
一.安装redis 这个不多说,网上有各种系统安装redis的操作, redis安装 二.创建sprigboot项目 这个也不多说,不会的前面有相关教程. 三.添加maven坐标 四.编写spr ...
- Docker整合dockerfly实现UI界面管理(单机版)
一.搜索镜像 docker search dockerfly 二.根据镜像使用排名(一般情况下拉取使用率最高的镜像名),我这里使用的是阿里云镜像地址 docker pull registry.cn-h ...
随机推荐
- Quartz.NET 2.x教程
第1课:使用Quartz第2课:工作和触发器第3课:关于工作和JobDetails的更多信息第4课:有关触发器的更多信息第5课:SimpleTriggers第6课:CronTriggers第7课:Tr ...
- Wannafly Camp 2020 Day 2J 邦邦的2-SAT模板
#include <bits/stdc++.h> using namespace std; int main() { int n; cin>>n; cout<<n& ...
- 打包Windowsform项目出现File 'Cognex.VisionPro3D.dll' targeting 'AMD64' is not compatible with the project's target platform 'x86'错误
错误信息: 个人理解此错误的大概意思是:打包的文件是64位的但是打包后的文件设置的是32位的,就出现冲突了. 解决方案:选择打包程序项目的属性窗口设置TargetPlatform属性为对应的值,本项目 ...
- linux - redis-trib.rb 命令详解
参考网站 http://www.cnblogs.com/ivictor/p/9768010.html 简介 redis-trib.rb是官方提供的Redis Cluster的管理工具,无需额外下载,默 ...
- chrome查看js报错Uncaught SyntaxError: Unexpected string
个人博客原文http://wenhaofan.com/article/20180912123136 js报错,最后发现是$(function(){})这部分()不完整 $(function({ $(& ...
- Unity3D制作3D虚拟漫游场景(二)
传送门: Unity3D制作3D虚拟漫游场景(一) -------------------------------------------------------------------------- ...
- Cron表达式及其使用注意事项
Cron表达式简介 Cron表达式全程Crontab表达式,是描述Crontab定时任务执行周期的一种语法格式.而Cron表达式严格上来说有许多特别的版本.如:Linux的.Spring的.Quart ...
- C#连接数据库时Appsettings 与connectionStrings的区别
C#连接数据库时Appsettings 与connectionStrings的区别 转载lscyo 最后发布于2018-08-08 18:38:04 阅读数 3366 收藏 展开 一..Appset ...
- 电脑进不去BIOS解决办法
把所有外设(主要是硬盘,包括装在主板上的固态硬盘)拆下来,拆下纽扣电池给主板放电,装回纽扣电池,重启F1进入BIOS. 最终查到原因,是固态那里出的问题,固态作为启动硬盘,被自己搞得有问题了,有两个启 ...
- 动态规划 ---- 最长公共子序列(Longest Common Subsequence, LCS)
分析: 完整代码: // 最长公共子序列 #include <stdio.h> #include <algorithm> using namespace std; ; char ...