redis——Java整合
redis官网
微软写的windows下的redis
我们下载第一个
额案后基本一路默认就行了
安装后,服务自动启动,以后也不用自动启动。
出现这个表示我们连接上了。
redis命令参考链接
Spring整合Redis
引入依赖
- spring-boot-starter-data-redis
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
配置Redis
- 配置数据库参数
# RedisProperties
spring.redis.database=11#第11个库,这个随便
spring.redis.host=localhost
spring.redis.port=6379#端口
- 编写配置类,构造RedisTemplate
这个springboot已经帮我们配了,但是默认object,我想改成string
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(factory);// 设置key的序列化方式template.setKeySerializer(RedisSerializer.string());// 设置value的序列化方式template.setValueSerializer(RedisSerializer.json());// 设置hash的key的序列化方式template.setHashKeySerializer(RedisSerializer.string());// 设置hash的value的序列化方式template.setHashValueSerializer(RedisSerializer.json());template.afterPropertiesSet();return template;}}
访问Redis
- redisTemplate.opsForValue()
- redisTemplate.opsForHash()
- redisTemplate.opsForList()
- redisTemplate.opsForSet()
- redisTemplate.opsForZSet()
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
public class RedisTests {@Autowiredprivate RedisTemplate redisTemplate;@Testpublic void testStrings() {String redisKey = "test:count";redisTemplate.opsForValue().set(redisKey, 1);System.out.println(redisTemplate.opsForValue().get(redisKey));System.out.println(redisTemplate.opsForValue().increment(redisKey));System.out.println(redisTemplate.opsForValue().decrement(redisKey));}@Testpublic void testHashes() {String redisKey = "test:user";redisTemplate.opsForHash().put(redisKey, "id", 1);redisTemplate.opsForHash().put(redisKey, "username", "zhangsan");System.out.println(redisTemplate.opsForHash().get(redisKey, "id"));System.out.println(redisTemplate.opsForHash().get(redisKey, "username"));}@Testpublic void testLists() {String redisKey = "test:ids";redisTemplate.opsForList().leftPush(redisKey, 101);redisTemplate.opsForList().leftPush(redisKey, 102);redisTemplate.opsForList().leftPush(redisKey, 103);System.out.println(redisTemplate.opsForList().size(redisKey));System.out.println(redisTemplate.opsForList().index(redisKey, 0));System.out.println(redisTemplate.opsForList().range(redisKey, 0, 2));System.out.println(redisTemplate.opsForList().leftPop(redisKey));System.out.println(redisTemplate.opsForList().leftPop(redisKey));System.out.println(redisTemplate.opsForList().leftPop(redisKey));}@Testpublic void testSets() {String redisKey = "test:teachers";redisTemplate.opsForSet().add(redisKey, "刘备", "关羽", "张飞", "赵云", "诸葛亮");System.out.println(redisTemplate.opsForSet().size(redisKey));System.out.println(redisTemplate.opsForSet().pop(redisKey));System.out.println(redisTemplate.opsForSet().members(redisKey));}@Testpublic void testSortedSets() {String redisKey = "test:students";redisTemplate.opsForZSet().add(redisKey, "唐僧", 80);redisTemplate.opsForZSet().add(redisKey, "悟空", 90);redisTemplate.opsForZSet().add(redisKey, "八戒", 50);redisTemplate.opsForZSet().add(redisKey, "沙僧", 70);redisTemplate.opsForZSet().add(redisKey, "白龙马", 60);System.out.println(redisTemplate.opsForZSet().zCard(redisKey));System.out.println(redisTemplate.opsForZSet().score(redisKey, "八戒"));System.out.println(redisTemplate.opsForZSet().reverseRank(redisKey, "八戒"));System.out.println(redisTemplate.opsForZSet().reverseRange(redisKey, 0, 2));}@Testpublic void testKeys() {redisTemplate.delete("test:user");System.out.println(redisTemplate.hasKey("test:user"));redisTemplate.expire("test:students", 10, TimeUnit.SECONDS);}
}
这样还是稍微有点麻烦,我们其实可以绑定key
// 多次访问同一个key@Testpublic void testBoundOperations() {String redisKey = "test:count";BoundValueOperations operations = redisTemplate.boundValueOps(redisKey);operations.increment();operations.increment();operations.increment();operations.increment();operations.increment();System.out.println(operations.get());}
redis——Java整合相关推荐
- 详解SSH框架和Redis的整合
为什么80%的码农都做不了架构师?>>> 一个已有的Struts+Spring+Hibernate项目,以前使用MySQL数据库,现在想把Redis也整合进去. 1. 相关Ja ...
- redis java客户端配置,Java的Redis客户端选择-jedis与Lettuce
Lettuce 和 Jedis 的定位都是Redis的client,所以他们当然可以直接连接redis server. Jedis在实现上是直接连接的redis server,如果在多线程环境下是非线 ...
- Redis java客户端操作
jedis jedis官方指定的redis java客户端,将其导入到pom.xml问价内 <!-- https://mvnrepository.com/artifact/redis.clien ...
- Redis Java调用
Redis Java调用 package com.stono.redis;import redis.clients.jedis.Jedis;public class RedisJava {public ...
- kettle清洗mysql数据_ETL工具Kettle使用以及与Java整合实现数据清洗
本文主要讲述kettle的使用和与Java整合,具体下载与安装请自行百度! kettle有两种脚本方式:转换和工作,工作中可以添加转换.以下以转换为例. 1.新建一个转换, 2.在工作中经常用到的是表 ...
- SSH框架和Redis的整合(1)
一个已有的Struts+Spring+Hibernate项目,以前使用MySQL数据库,现在想把Redis也整合进去. 1. 相关Jar文件 下载并导入以下3个Jar文件: commons-pool2 ...
- redis java操作
Redis Java连接操作 连接到Redis服务器 import redis.clients.jedis.Jedis; public class RedisJava {public static v ...
- JAVA整合腾讯COS(拿来即用)
JAVA整合腾讯COS实现文件上传 1.开通腾讯云对象存储服务 https://console.cloud.tencent.com/cos5 2.创建存储桶 3.密钥管理,新建密钥 4:在项目yml文 ...
- Redis Java Client选型-Jedis Lettuce Redisson
目录 1. 目标 2. 选型过程 2.1. 待选集合 2.2. 对比列表 2.3. 确定选型的考虑角度 1. 目标 针对redis java client,从多角度进行选型对比,以便选择符合业务要求的 ...
最新文章
- Alpha阶段项目总结
- CentOS7 64位下 MySQL5.7的安装与配置(YUM)
- 第十一届蓝桥杯赛后总结 —— 两年征战蓝桥,惜败来年再战。
- python在线投票系统 统计票数_python投票统计程序,统计序列中各个数值的份数,字典的应用。...
- Method Swizzling 处理一类简单的崩溃
- 即将从TechReady5归来
- 微信wechat.class.php,laravel使用组件实现微信网页授权登入
- 配置WINDOWS群集
- 【剑指offer】35、复杂链表的复制
- Git(9)-- 远程仓库的使用
- Unity 接入科大讯飞语音识别及语音合成
- DDC及EDID内容简介
- vi打开GBK编码文件乱码问题
- 如何检测浏览器是否安装了Adblock,uBlock Origin,Adguard,uBlock等广告屏蔽插件
- 金融资产管理公司 不良资产的发言人
- windows如何设置软件开机启动
- 基于单片机的防盗报警监控系统设计(#0401)
- JAVA国际化 - Eason Jiang - 博客园
- win10文件夹加密_Win10 系统优化软件 Windows 10 Manager v3.2.0
- android webview goback 刷新,解决webview调用goBack()返回上一页自动刷新闪白的情况
热门文章
- 零基础不建议学前端_web前端培训心得:零基础怎样学好web前端
- saphana服务器硬件评估,华为SAP HANA一体机:你身边的数据计算专家
- 编译mediastreamer2/ffmpeg/linphone(x86平台)
- linux内核I2C子系统学习(一)
- c++ char*初始化_C开发实战-深入理解指针
- tortoisegit图标消失_TortoiseGit文件夹和文件图标不显示解决方法
- python去空格的函数_Python怎么去掉最后的空格
- 移动app测试的多样性_快速搞定APP移动端自动化测试
- android studio 显示图形_显示服务器实现(一)
- 【转】C# 动态对象(dynamic)的用法