博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot整合redis框架
阅读量:2161 次
发布时间:2019-05-01

本文共 2914 字,大约阅读时间需要 9 分钟。

4.0.0
com.learn
springboot2.0-redis
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.5.12.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-redis
###########################################################Redis (RedisConfiguration)########################################################spring.redis.database=0spring.redis.host=localhostspring.redis.port=6379spring.redis.password=123456spring.redis.pool.max-idle=8spring.redis.pool.min-idle=0spring.redis.pool.max-active=8spring.redis.pool.max-wait=-1spring.redis.timeout=5000
package com.learn.service;import java.util.Set;import java.util.concurrent.TimeUnit;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.stereotype.Component;//  springboot 2.0 整合redis@Componentpublic class RedisService {	@Autowired	private StringRedisTemplate stringRedisTemplate;	public void set(String key, Object object, Long time) {		// 让该方法能够支持多种数据类型存放		if (object instanceof String) {			setString(key, object);		}		// 如果存放时Set类型		if (object instanceof Set) {			setSet(key, object);		}		// 设置有效期		if (time != null) {			stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);		}	}	public void setString(String key, Object object) {		String value = (String) object;		// 存放string类型		stringRedisTemplate.opsForValue().set(key, value);	}	public void setSet(String key, Object object) {		Set
valueSet = (Set
) object; for (String string : valueSet) { stringRedisTemplate.opsForSet().add(key, string); } } public String getString(String key) { return stringRedisTemplate.opsForValue().get(key); }}
package com.api.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.learn.service.RedisService;@RestControllerpublic class IndexController {	@Autowired	private RedisService redisService;	@RequestMapping("/setString")	public String setString(String key, String object) {		redisService.set(key, object, 60l);		return "success";	}	@RequestMapping("/get")	public String get(String key) {		return redisService.getString(key);	}}
package com;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class App {	public static void main(String[] args) {		SpringApplication.run(App.class, args);	}}
localhost:8080/setString?key=leon&object=123456localhost:8080/get?key=leon

 

转载地址:http://mfkzb.baihongyu.com/

你可能感兴趣的文章
用 Grid Search 对 SVM 进行调参
查看>>
用 Pipeline 将训练集参数重复应用到测试集
查看>>
PCA 的数学原理和可视化效果
查看>>
机器学习中常用评估指标汇总
查看>>
什么是 ROC AUC
查看>>
Bagging 简述
查看>>
详解 Stacking 的 python 实现
查看>>
简述极大似然估计
查看>>
用线性判别分析 LDA 降维
查看>>
用 Doc2Vec 得到文档/段落/句子的向量表达
查看>>
使聊天机器人具有个性
查看>>
使聊天机器人的对话更有营养
查看>>
一个 tflearn 情感分析小例子
查看>>
attention 机制入门
查看>>
手把手用 IntelliJ IDEA 和 SBT 创建 scala 项目
查看>>
双向 LSTM
查看>>
GAN 的 keras 实现
查看>>
AI 在 marketing 上的应用
查看>>
Logistic regression 为什么用 sigmoid ?
查看>>
Logistic Regression 为什么用极大似然函数
查看>>