- Redis 是什么?
- 如何使用Redis?
- Redis 基础
Redis 是什么?
- nosql 数据库,内存数据库,常用作缓存
- 全是基于 键值 思想
- 我的理解,Redis 的key , 是用来表示存储的对象的。相当于JAVA中对象的引用;key 都是字符串
- Redis 的value 有5种不同的数据结构
- 字符串类型
- 哈希类型: map 格式
- 列表类型: list 格式,支持重复元素
- 集合类型: set 格式,不允许重复元素
- 有序集合类型 sortedset: 不允许重复元素,且元素有顺序
如何使用Redis
- 下载解压直接使用
- 打开服务端:redis-server.exe
- 打开客户端: redis-cli.exe
- 再Java中使用: Jedis
- 下载jedis的jar包并导入
- 获取链接
- Jedis jedis = new Jedis(“localhost”,6379);
- 直接使用
- jedis.set(“username”,”zhangsan”);
- 关闭连接
- jedis.close();
- 可以写一个工具类
- 读取jedis配置文件,放到 JedisPoolConfig config 对象中
- 初始化jedisPool,通过jedisPoo 获得连接 ```java
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();
}
}
```