day-27 redis

  • 在文件内部 / 会搜东西
  • redis持久化rdb快照有时间间隔会丢数据但简单,aof数据大时候特别卡但及时
  • Redis事务不算事务没有回滚,只能是批量执行
  • set,list无需有序,不可重复可重复
  • 一般使用string存,要记忆
  • arr查询快,link增删快
  • 执行可执行文件需要./
  • nosql数据库交换数据快,一般在内存中
  • redis需要和修改后的配置文件一起执行:./redis-server redis.conf (将redis.conf文件中的daemonize从false修改成true表示后台启动)
  • 只要在Linux上安软件,相对远程开放必须将端口号保存到防火墙中
  • 任何对象都能转成字符串(json)


    Paste_Image.png
  • 正常开发池子不会关,jedis资源会关
  • 使用bundle获取资源没有后缀名
---------------JedisTool ------
public class JedisTool {
    private static JedisPoolConfig pool=null;
    private static JedisPool jedisPool=null;
    static{
        ResourceBundle bundle =  ResourceBundle.getBundle("Jdies");
                pool = new JedisPoolConfig();
        pool.setMaxIdle(Integer.parseInt(bundle.getString("maxIdle")) );
        pool.setMinIdle(Integer.parseInt(bundle.getString("minIdle")));
        pool.setMaxTotal(Integer.parseInt(bundle.getString("maxTotal")));
         jedisPool = new JedisPool(pool, bundle.getString("url"),Integer.parseInt(bundle.getString("port")));
         System.out.println(Integer.parseInt(bundle.getString("maxIdle")) +"==");
    }
    public static JedisPool getPool () {
        return jedisPool;
    }
    public static Jedis getJedis() {
        return getPool().getResource();
    }
}
-------------------获取资源另一种------
InputStream in = JedisPoolUtils.class.getClassLoader().getResourceAsStream("redis.properties");
        Properties pro = new Properties();
        try {
            pro.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        //获得池子对象
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(Integer.parseInt(pro.get("redis.maxIdle").toString()));//最大闲置个数
        poolConfig.setMinIdle(Integer.parseInt(pro.get("redis.minIdle").toString()));//最小闲置个数
        poolConfig.setMaxTotal(Integer.parseInt(pro.get("redis.maxTotal").toString()));//最大连接数
        pool = new JedisPool(poolConfig,pro.getProperty("redis.url") , Integer.parseInt(pro.get("redis.port").toString()));
--------------------------
Jedis jedis =JedisTool.getJedis();
        jedis.set("xxx", "lisi");
        System.out.println(jedis.get("xxx"));

1.Redis存取字符串

  • set key value;get key
  • incr key:将指定的key的value原子性的递增1.如果该key不存在,其初始值 为0,在incr之后其值为1。如果value的值不能转成整型,如hello,该操作将执 行失败并返回相应的错误信息。
  • decr key:将指定的key的value原子性的递减1.如果该key不存在,其初始值 为0,在incr之后其值为-1。如果value的值不能转成整型,如hello,该操作将执 行失败并返回相应的错误信息。
  • append key value:如果该key存在,则在原有的value后追加该值;如果该 key 不存在,则重新创建一个key/value
    2.存取一共五种类型
Paste_Image.png

3.redis 有前后台,正常关闭服务redis-cli shutdown,开启redis-service redis.config是一起开,同时开启客户端redis-cli,exit,quit是关闭客户端但关不了服务器
一般执行程序都加./

4.keys的通用操作

  • keys pattern:获取所有与pattern匹配的key返回所有与该key匹配的keyso*表示任意一个或多个字符,?表示任意一个字符(keys *)
  • del keyl key2....二:删除指定的key
  • exists key:判断该key是否存在,1代表存在,0代表不存在
  • rename key new key:为当前的key重命名
  • expire key 时间:设置过期时间,单位:秒
  • ttl key:获取该key所剩的超时时间,如果没有设置超时,返回一1如果返回一2表示超时不存在。
  • type key:获取指定key的类型。该命令将以字符串的格式返回。
    返回的字符串为string. list, set. hash和:set,如果key不存在返回none

5.数据库

  • 一个redis实例最多可提供16个数据库,下标从0到15,客户端默认连接第0号数据库,也可以通过select选择连接哪个数据库,select 1
  • 将newkey移植到1号库 move new key 1:将当前库的key移植到1号库中
  • 还有消息订阅与发布,事务,持久化(rdb,aof)

你可能感兴趣的:(day-27 redis)