首页 > 其他 > 详细

Redis

时间:2019-12-29 15:40:33      阅读:57      评论:0      收藏:0      [点我收藏+]

概念

非关系型数据库:redis将数据以键值对的形式存储,数据之间没有关系,存储在内存中

NOSQL系列:redis,hbase . . .

数据结构:键值对,key都是字符串,value分5种不同的数据结构

  • String(字符串类型),map(哈希类型),linkedlist(列表类型),set(集合类型),sortedset(有序集合类型)

缓存的工作流程

  • 从缓存中获取数据
    • 有数据
      • 返回数据
    • 没有数据
      • 从数据库查询
      • 将数据放入缓存
      • 返回数据

环境搭建

  • 解压直接使用
    • redis.windows.conf:配置文件
    • redis.cli.exe:客户端
    • redis.server.exe:服务器端

命令行操作

  • String
    • 存储:set key value
    • 获取:get key
    • 删除:del key
  • Hash
    • 存储:hset key field value
    • 获取指定field的value:hget key field
    • 获取所有field和value:hgetall key
    • 删除:hdel key field
  • List
    • 存储元素到左边:lpush key value
    • 存储元素到右边:rpush key value
    • 范围获取:lrange key start end
    • 删除并返回:lpop key / rpop key
  • Set(不允许重复元素)

    • 存储:sadd key value
    • 获取所有元素:smembers key
    • 删除某一元素:srem key value
  • Sortedset

    • 存储:zadd key score value
    • 获取:zrange key start end
    • 删除:zrem key value
  • 通用

    • keys * :查询所有的键
    • type key :获取对应value的类型
    • del key :删除指定的键值对
  • 持久化

    • RDB:默认方式,不需配置

      • 在一定的间隔时间中,检测key的变化情况,然后持久化数据
      1. 编辑redis.windows.conf文件

        save 900 1 # after 900 sec (15 min) if at least 1 key changed

        save 300 10 # after 300 sec (5 min) if at least 10 key changed

        save 60 10000 # after 60 sec (1 min) if at least 10000 key changed

      2. 使用命令行启动服务端

        redis-server.exe redis.windows.conf

    • AOF:日志记录方式

      • 可以记录每一条命令的操作,在每一次命令操作后持久化数据
      1. 编辑redis.windows.conf文件

        appendonly no - - > appendonly yes

      2. appendfsync always : 每一次操作都进行持久化

        appendfsync everysec : 每隔一秒进行持久化

        appendfsync no : 不进行持久化

Java客户端 Jedis

  • 基本步骤

    1. 导入jar包

    2. 编写测试类

      // 1.获取连接
      Jedis jedis = new Jedis("localhost", 6379); //默认参数为("localhost", 6379)
      // 2.操作
      jedis.set("username", "root");
      // 3.关闭连接
      jedis.close();
  • 各种数据结构的操作方法

    //String
    jedis.set("username", "root");              // 存储
    String username = jedis.get("username");    // 获取
    jedis.setex("invitecode", 60, "123456");    // 存储并在60秒后自动删除
    //Hash
    jedis.hset("user", "username", "root");             // 存储
    jedis.hset("user", "password", "1234");
    String username = jedis.hget("user", "username");   //获取单个值
    Map<String, String> user = jedis.hgetAll("user");   //获取user中所有键值对
    //List
    jedis.lpush("mylist", "a", "b", "c");       //从左边存入
    jedis.rpush("mylist", "a", "b", "c");       //从右边存入,最终结果为 c b a a b c
    List<String> mylist = jedis.lrange("mylist", 0, -1);    //取出指定范围的list,-1为取全部
    String str1 = jedis.lpop("mylist");         //弹出最左边的元素
    String str2 = jedis.rpop("mylist");         //弹出最右边的元素
    //Set
    jedis.sadd("myset", "java", "php", "c++");      //存储
    Set<String> myset = jedis.smembers("myset");    //获取
    //SortedSet
    jedis.zadd("mysortedset", 5, "ailment");                        //存储
    Set<String> mysortedset = jedis.zrange("mysortedset", 0, -1);   //取出指定范围
  • Jedis连接池

    • 使用

      // 0.创建配置对象,可选,有默认值
      JedisPoolConfig config = new JedisPoolConfig();
      config.setMaxTotal(50);
      config.setMaxIdle(10);      //最大空闲连接
      // 1.创建Jedis连接对象
      JedisPool jedisPool = new JedisPool("localhost", 6379);     //参数有默认值
      // 2.获取连接
      Jedis jedis = jedisPool.getResource();
      // 3.使用
      . . .
      // 4.关闭,归还到连接池中
      jedis.close();
  • 抽取工具类

    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.setMaxTotal(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();
        }
    }
    # jedis.properties
    host=127.0.0.1
    port=6379
    maxTotal=50
    maxIdle=10

用Redis优化数据库查询

//从缓存中查询
Jedis jedis = JedisPoolUtils.getUtils();
String province_json = jedis.get("province");
//如果缓存中没有
if(province_json == null || province_json.length() == 0){
    //从关系型数据库查询
    List<Province> ps = dao.findAll();
    ObjectMapper mapper = new ObjectMapper();
    try{
        province_json = mapper.writeValueAsString(ps);
    }catch(JsonProcessingException e){
        e.printStackTrace();
    }
    //将查询结果存入缓存
    jedis.set("provice", province_json);
    jedis.close();
}
//返回查询结果
return province_json;
  • 数据库的表执行DML操作后需要将redis缓存数据清空

Redis

原文:https://www.cnblogs.com/whteway/p/12115134.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!