引入依赖

 compile 'org.springframework.boot:spring-boot-starter-data-redis'

使用redis有两种方法

1.Jedis

Jedis jedis = new Jedis("localhost");

2.RedisTemplate

@Autowired
private RedisTemplate redisTemplate;

如果使用RedisTemplate的话,要在application.properties中配置信息,这里我使用Jedis比较简单

redis的自动配置

在application.properties文件下

#redis的springboot的自动配置
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
# 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

Jedis使用

package com.test.booleanjava.helloRS.util;

import redis.clients.jedis.Jedis;

/**
* @author booleanjava
* Date: 2019/7/2 19:48
* description:redis的工具类
*/
public class RedisUtil {
static Jedis jedis = new Jedis("localhost"); /**
* 插入key,如果存在就更新
* @param key
* @param value
* @return
*/
public static String set(String key, String value){
return jedis.set(key, value);
} /**
* 获取key的值
* @param key
* @return
*/
public static String get(String key) {
return jedis.get(key);
} /**
* 删除key
* @param key
* @return
*/
public static Long del(String key){
return jedis.del(key);
} /**
* 设置一个有过期时间的key(秒)
* @param key
* @param seconds
* @param value
* @return
*/
public static String setex(final String key, final int seconds, final String value){
return jedis.setex(key, seconds, value);
} /**
* 如果不存在就执行操作,用作简单分布式锁
*
* @param key
* @param value
* @return true表示执行,false表示没有执行
*/
public static Boolean setnx(final String key, final String value){
return jedis.setnx(key, value) == 1;
} }

RedisTemplates使用

package com.test.booleanjava.helloRS.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component; /**
* @author boolean
* Date: 2019/7/2 19:48
* description:
*/ @Component
public class Redisplus { @Autowired
private RedisTemplate redisTemplate; public void set(String key, String value){
redisTemplate.opsForValue().set(key, value); }
}

测试

package com.test.booleanjava.helloRS.controller;

import com.test.booleanjava.helloRS.entity.User;
import com.test.booleanjava.helloRS.util.Redisplus;
import com.test.booleanjava.helloRS.service.IUserService;
import com.test.booleanjava.helloRS.util.RedisUtil;
import com.test.base.core.util.LogUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.Date; /**
* @author booleanjava
* Date: 2019/7/2 19:48
* description:
*/
@RestController
@RequestMapping("/helloRS/redisHello")
public class RedisHello {
private final static Logger logger = LoggerFactory.getLogger(RedisHello.class); private final static String USERKEY = "com.test.booleanjava.helloRS.controller.setex";
private final static String LOCKKEY = "com.test.booleanjava.helloRS.controller.lock"; @Autowired
private IUserService iUserService; @Autowired
private Redisplus redisplus; @Autowired
private RedisTemplate redisTemplate;
RedisSerializer redisSerializer =new StringRedisSerializer(); @RequestMapping("/hello")
public String hello(){
LogUtil.info("redis的展示:[{}]", redisTemplate);
return "hello, redis";
} @RequestMapping("/set")
public String set(){
Date date = new Date();
redisTemplate.setKeySerializer(redisSerializer);
redisTemplate.opsForValue().set("q", "1");
redisTemplate.opsForValue().get("q");
System.out.println(redisTemplate.opsForValue().get("q"));
RedisUtil.set("a1", String.valueOf(1));
logger.info("redis的展示:[{}]", redisTemplate);
return "hello, set一下redis";
} @RequestMapping("/setex")
public String setex( ){
// String key = "1min";
// int seconds = 10;
// String value = "陈";
// RedisUtil.setex(key, seconds, value);
// String rs = RedisUtil.get(key);
// logger.info("获取的值:[{}]", rs);
String value = RedisUtil.get(USERKEY);
if (value != null) {
logger.info("缓存的user值:[{}]", value);
return value;
}
User user = iUserService.query().eq("name", "chen").one();
logger.info("user的值:[{}]",user.toString());
if (user != null ) {
RedisUtil.setex(USERKEY, 60, user.toString());
}
return "hello,booleanjava,设置了有时限的key";
} @RequestMapping("/del")
public String del(String key) {
redisTemplate.delete(key);
return "hello, del一下redis";
} /**
* 做分布锁,
*先加锁,写业务,最后解锁
* @return
*/
@RequestMapping("/lock")
public String lock() {
//加锁
RedisUtil.setnx(LOCKKEY,LOCKKEY);
//写业务代码,一人我饮酒醉 //解锁
RedisUtil.del(LOCKKEY); return "hello, lock一下redis";
}
}

源码

https://github.com/blackdogss/HelloWorld/tree/master/helloRS

深入

背景

互联网公司大部分通常使用myslq作为数据库存储数据,但是mysql存数据是以影响IO为代价的,所以mysql是系统的常见瓶颈,为解决这个问题,redis这种非关系型数据库就出现了,存在即合理。redis喜欢在内存操作,比mysql在磁盘瞎忙高效多了,因此深受人们喜爱。

数据结构

redis有五种数据结构

1.String 字符串

2.Hash哈希

3.List列表

4.Set集合

5.Sorted Set

最常用的就是String类型,通常使用它做缓存,减轻直接访问数据库的压力。Hash的话可以用来做用户id,List可以用来做粉丝列表,Set的话可以做共同好友,Sorted Set可以做排行榜。

分布式锁

redis处理上面列举的例子,还有就是可以做分布式锁,在分布式系统中,接口面临的是多进程多线程访问,如果依赖java的锁是不能解决问题的,因为进程之间不共享内存;利用数据库加锁又显得笨重,因此还得用redis来加锁。redis怎么加锁,主要还是利用setnx命令,该命令作用是如果key存在就不执行操作,不存在的话就设置value,这种特性就是为锁打造的啊。

公众号

redis整合springboot的helloworld的更多相关文章

  1. Redis实战--Redis整合SpringBoot示例

    echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.--这才是真正的堪称强大!!! 该文章 ...

  2. SpringBoot+Redis整合

    SpringBoot+Redis整合 1.在pom.xml添加Redis依赖 <!--整合Redis--> <dependency> <groupId>org.sp ...

  3. SpringBoot日记——Redis整合

    上一篇文章,简单记录了一下缓存的使用方法,这篇文章将把我们熟悉的redis整合进来. 那么如何去整合呢?首先需要下载和安装,为了使用方便,也可以做环境变量的配置. 下载和安装的方法,之前有介绍,在do ...

  4. Shiro整合springboot,freemaker,redis(含权限系统完整源码)

    区块链技术联盟 2018-02-08 17:06:40 目录 一.导语 二.shiro功能介绍 三.shiro详解 四.shiro实战案例分享 五.系统配置 六.其他 一.导语 今天推荐给大家一个非常 ...

  5. springboot和Redis整合

    springboot简化了许多的配置,大大提高了使用效率.下面介绍一下和Redis整合的一些注意事项. 首先介绍单机版的redis整合. 1.第一步当然是导入依赖 <dependency> ...

  6. redis(七)---- SpringBoot和redis整合

    SpringBoot和Redis整合非常简单 添加pom依赖 <dependency> <groupId>org.springframework.boot</groupI ...

  7. SpringBoot + Mybatis + Redis 整合入门项目

    这篇文章我决定一改以往的风格,以幽默风趣的故事博文来介绍如何整合 SpringBoot.Mybatis.Redis. 很久很久以前,森林里有一只可爱的小青蛙,他迈着沉重的步伐走向了找工作的道路,结果发 ...

  8. canal整合springboot实现mysql数据实时同步到redis

    业务场景: 项目里需要频繁的查询mysql导致mysql的压力太大,此时考虑从内存型数据库redis里查询,但是管理平台里会较为频繁的修改增加mysql里的数据 问题来了: 如何才能保证mysql的数 ...

  9. Redis-基本概念、java操作redis、springboot整合redis,分布式缓存,分布式session管理等

    NoSQL的引言 Redis数据库相关指令 Redis持久化相关机制 SpringBoot操作Redis Redis分布式缓存实现 Resis中主从复制架构和哨兵机制 Redis集群搭建 Redis实 ...

随机推荐

  1. 最通俗易懂的方式让你理解 Swift 的函数式编程

    函数式编程(Functional Programming)是相对于我们常用的面向对象和面向过程编程的另外一种开发思维方式,它更加强调以函数为中心.善用函数式编程思路,可以对我们的开发工作有很大的帮助和 ...

  2. Android Intent传递对象摘要

    效果: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaG9uZ3NoZW5ncGVuZw==/font/5a6L5L2T/fontsize/400/fil ...

  3. Template简介

    分类   ControlTemplate ItemsPanelTemplate DataTemplate 样式Style和模板Template对比 Style:样式,风格Template:模版,某种控 ...

  4. 读BeautifulSoup官方文档之与bs有关的对象和属性(3)

    上一节说到.string的条件很苛刻, 如果某个tag里面包含了超过一个children, 就会返回None, 但是这里提供另外一种方式 .strings, 它返回的是一个generator, 比如对 ...

  5. symfony 数据库表生成实体、迁移数据库

    从数据库表生成实体 1. 由数据库生成模型: php bin/console doctrine:mapping:convert --from-database yml D:\db\ D:\test_b ...

  6. discuz电脑访问手机版域名怎么跳转到电脑版本

    用discuz论坛访问手机版本的域名不会自动跳转到电脑版本,而是会跳转到域名+misc.php?mod=mobile体验很不好.现提供修改方法:打开论坛根目录找到文件./source/class/di ...

  7. ELINK离线编程器常见问题

    Q1 编程器是否可以接JTAG JTAG接口已经包含SWD接口引脚,按以下引脚对应接线即可: SWDIO->目标板JTAG 的JTMS SWCLK->目标板JTAG 的JTCK Q2 PC ...

  8. 微信小程序把玩(二十九)video组件

    原文:微信小程序把玩(二十九)video组件 视频播放组件与图片加载组件也没啥差别,使用起来也没啥注意的 重要属性: wxml <!--监听button点击事件--> <button ...

  9. 微信小程序把玩(三十六)Storage API

    原文:微信小程序把玩(三十六)Storage API 其实这个存储在新建Demo的时候就已经用到了就是存储就是那个logs日志,数据存储主要分为同步和异步 异步存储方法: 存数据 wx.setStor ...

  10. 遍历所有的XML

    XmlElement rootElement = doc.DocumentElement; foreach (XmlElement childElement in rootElement) { //C ...