官网下载: http://redis.io/download
使用资料安装包
解压到D盘的software目录下。
使用对应位数操作系统文件夹下面命令启动redis
其中:
redis-server.exe 服务启动程序
redis-cli.exe 客户端命令行工具
redis.conf 服务配置文件
通过redis-server.exe启动服务,默认端口 6379
通过redis-cli.exe 启动客户端工具
表示连接成功!
2.2、Redis注册服务
如果每次启动服务,比较麻烦,能否把redis服务注册到windows上?
- 注册服务:
redis-server --service-install redis.windows.conf --loglevel verbose
- 卸载服务:
redis-server --service-uninstall
- 启动Redis:
redis-server --service-start
停止Redis:
redis-server --service-stop
2.3、Redis基本命令
set
key value,如果该key存在则进行覆盖操作。
get
key,根据key获取值
KEYS 获取符合规则的key
EXISTS 判断一个key是否存在
DEL 删除键
TYPE 获取key的数据类型
HELP 帮助命令
2.4、Redis数据类型之字符串
INCR 递增
INCRBY 递增指定的整数
DECR 递减
APPEND 追加
STRLEN 获取字符串的长度
2.5、Redis之生存时间
清除生存时间
剩余生成时间:
-1:永久生效
-2:键不存在
2.6、Jedis 使用和redis图形界面工具
2.6.1 jedis的基本使用
通过java程序操作redis, 使用jedis 工具。
网址: https://github.com/xetorthio/jedis
根据上面的案例编写测试代码:
第一步:在bos-parent中导入坐标
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
第二步:com.czxy.bos.JedisTest.java
public class JedisTest {
public static void main(String[] args) {
//连接localhost,默认端口是6379。
Jedis jedis = new Jedis("localhost");
jedis.set("school", "大学");
System.out.println(jedis.get("school"));
}
}
第三步:其他使用
- 取值
Jedis jedis=new Jedis("127.0.0.1");
String info= jedis.get("student");
- 删除
Jedis jedis=new Jedis("127.0.0.1");
jedis.del("student");
- 使用jedis连接池
@SuppressWarnings("resource")
public static void main(String[] args) {
//创建连接池config对象
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(10);
config.setMaxTotal(50);
//创建jedis连接池
JedisPool pool = new JedisPool(config , "127.0.0.1", 6379 );
//获取连接
Jedis jedis = pool.getResource();
jedis.set("itheima12", "好样的,你竟然找了男的做女朋友");
String result = jedis.get("itheima12");
System.out.println(result);
jedis.close();
}
- 设置key有效周期
//jedis.set("school", "大学");
jedis.setex("school", 30 , "大学");//30:表示30秒钟后过期
- 使用redis-cli.exe查看测试
2.6.2 安装redis图形化界面
类似SQLyog操作mysql数据库
打开:“RedisDesktopManager”添加连接
添加信息
查下key和value
其中:TTL 是redis的key 有效时间,显示- ,说明没有设置key的有效期
2.7、Spring Data Redis 使用
SprignBoot Data家族是Spring中专业的跟数据打交道的技术。
Redis是一种nosql数据库,在开发中常用做缓存。Jedis是Redis在Java中的redis- client。spring把专门的数据操作独立封装在spring-data系列中,spring-data-redis自然是针对Redis的独立封装了,而且Spring Data Redis是对redis客户端(如jedis)的高度封装,支持多种客户端。
官网: http://projects.spring.io/spring-data-redis/
REDIS (RedisProperties) redis基本配置;
- common-parent:pom.xml中添加redis坐标
<!-- 配置使用redis启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- 配置信息
########################################################
###REDIS (RedisProperties) redis基本配置;
########################################################
# database name
spring.redis.database=0
# server host1
spring.redis.host=127.0.0.1
# server password
#spring.redis.password=
#connection port
spring.redis.port=6379
# pool settings ...
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=-1
# name of Redis server
#spring.redis.sentinel.master=
# comma-separated list of host:port pairs
#spring.redis.sentinel.nodes=