Redis
下载:
百度云下载 提取码:o1ui
Github下载
概念:一款高性能的NOSQL系列的非关系型数据库
命令:
redis存储的是:key,value格式的数据,其中key都是字符串,value有5中不同的数据结构
value的数据结构
1.字符串类型 string
2.哈希类型 hash :相当于map集合
3.列表集合 list :linkedlist格式
4.集合类型 set
5.有序集合类型 sortedset
字符串类型string
1.存储 set key value
2.获取 get key
3.删除 del key
哈希类型 hash
1.存储 hset key field value
2.获取 hget key field hgetall key 获取所有的键和值
3.删除 hdel key field
列表类型 list
1.lpush key value 将元素加入列表左边
2.rpush key value 将元素加入列表右边
3.lrange key start end 获取范围
4.lpop key 删除列表最左边的元素,并将元素返回
5.rpop key 删除列表最右边的元素
集合类型 set
1.存储:sadd key value
2.获取:smembers key :获取集合中所有元素
3.删除:srem key value :删除集合的某个元素
有序集合类型 sortedset
1.存储:zadd key score value
2.获取:zrange key start end
3.删除:zrem key value
通用命令:
1.keys * :查询所有的键
2.type key:获取键对应的value的类型
3.del key:删除指定的key value
持久化存储
Java客户端Jedis
Jedis是一款java操作redis数据库的工具
Jedis连接池:JedisPool
1.创建JedisPool连接池对象
2.调用getResource()方法获取Jedis的连接
工具类代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| package cn.jedis.utlis;
import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig;
import java.io.IOException; import java.io.InputStream; import java.util.Properties;
/* JedisPool工具类 加载配置文件,配置连接池参数 提供获取连接的方法 */ public class JedisPoolUtils { private static JedisPool jedisPool;
static { //读取配置文件 InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties"); //创建一个Properties对象 Properties pro = new Properties(); //关联文件 try { pro.load(is); } catch (IOException e) { e.printStackTrace(); } //获取数据,设置到JedisPoolConfig中 JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal"))); config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));
//初始化JedisPool jedisPool = new JedisPool(config,pro.getProperty("host"),Integer.parseInt(pro.getProperty("port"))); } //获取连接 public static Jedis getJedis(){ return jedisPool.getResource(); } }
|
SpringBoot整合Redis
导入Redis启动器
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
|
在配置文件中配置Redis参数,默认存在,不配也可
1 2 3 4 5
| spring: redis: host: localhost port: 6379
|
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| package com.gem;
import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.List; import java.util.Set;
@SpringBootTest public class RedisTest {
@Autowired private StringRedisTemplate stringRedisTemplate;
@Test public void test1(){
stringRedisTemplate.boundHashOps("h_key").put("name","张三"); stringRedisTemplate.boundHashOps("h_key").put("age","12");
Set set = stringRedisTemplate.boundHashOps("h_key").keys(); System.out.println(set);
List list = stringRedisTemplate.boundHashOps("h_key").values(); System.out.println(list);
stringRedisTemplate.boundListOps("l_key").leftPush("c"); stringRedisTemplate.boundListOps("l_key").leftPush("b"); stringRedisTemplate.boundListOps("l_key").leftPush("a");
List l_key = stringRedisTemplate.boundListOps("l_key").range(0,-1); System.out.println(l_key);
stringRedisTemplate.boundSetOps("s_key").add("a","b","c"); Set s_key = stringRedisTemplate.boundSetOps("s_key").members(); System.out.println(s_key);
stringRedisTemplate.boundZSetOps("z_key").add("a",10); stringRedisTemplate.boundZSetOps("z_key").add("b",40); stringRedisTemplate.boundZSetOps("z_key").add("c",30);
System.out.println(stringRedisTemplate.boundZSetOps("z_key").range(0,-1));
} }
|