• 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 {

  1. private static JedisPool jedisPool;
  2. static{
  3. //读取配置文件
  4. InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
  5. //创建Properties对象
  6. Properties pro = new Properties();
  7. //关联文件
  8. try {
  9. pro.load(is);
  10. } catch (IOException e) {
  11. e.printStackTrace();
  12. }
  13. //获取数据,设置到JedisPoolConfig中
  14. JedisPoolConfig config = new JedisPoolConfig();
  15. config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
  16. config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));
  17. //初始化JedisPool
  18. jedisPool = new JedisPool(config,pro.getProperty("host"),Integer.parseInt(pro.getProperty("port")));
  19. }
  20. /**
  21. * 获取连接方法
  22. */
  23. public static Jedis getJedis(){
  24. return jedisPool.getResource();
  25. }

}

```