一路繁花似锦绣前程
失败的越多,成功才越有价值

导航

 

一、入门

一、数据结构
    1、字符串类型:string
    2、哈希类型:hash(map格式)
    3、列表类型:list(linkedlist格式)
    4、集合类型:set(不允许重复元素)
    5、有序集合类型:sortedset(不允许重复元素,且元素按score从小到大排序)
二、操作
    1、字符串类型(string1、存储:set key value
        2、获取:get key
        3、删除:del key
    2、哈希类型(hash)
        1、存储:hset key field value
        2、获取:
            * hget key field
            * hgetall key
        3、删除:hdel key field
    3、列表类型(list)
        1、存储:
            * lpush key value:元素从左加入列表
            * rpush key value:元素从右加入列表
        2、获取:lrange key start end:范围获取
            lrange key 0 -1:表示获取所有元素
        3、删除:
            * lpop key:从左边开始删除元素
            * rpop key:从右边开始删除元素
    4、集合类型(set1、存储:sadd key value
        2、获取:smembers key:获取set集合中所有元素
        3、删除:srem key value:删除set集合中的某个元素
    5、有序集合类型(sortedset)
        1、存储:zadd key score value
        2、获取:zrange key start end
            zrange key 0 -1 withscores:表示获取所有元素,且显示score
        3、删除:zrem key value
    6、通用命令
        1、keys *:获取所有的键
        2、type key:获取键对应的value的类型
        3、del key:删除指定的key value
三、持久化
    1、命令
        redis-server.exe redis.windows.conf
    2、方式(配置redis.windows.conf)
        * RDB:
            save 时间(S) 存储次数
        * AOF:
            appendonly yes:(yes开启AOF、no关闭AOF)
            appendfsync always:(always每次存储执行持久化、everysec每秒执行持久化、no不进行持久化)

 

二、jedis

public class JedisPractice {
    public static void main(String[] args) {
/*
一、操作
    1、字符串类型(string)
        jedis.set("name", "mengmeiqi");
        String name = jedis.get("name");
        //第二个参数为过期时间(S)
        jedis.setex("wife",10,"mengmeiqi");
    2、哈希类型(hash)
        jedis.hset("person", "name", "mengmeiqi");
        jedis.hset("person", "age", "18");
        jedis.hset("person", "sex", "girl");
        jedis.hget("person", "name");
        Map<String, String> person = jedis.hgetAll("person");
        Set<String> fields = person.keySet();
        for (String field : fields) {
            System.out.println(field + ":" + person.get(field));
        }
    3、列表类型(list)
        jedis.lpush("wife", "a", "b", "c");
        jedis.rpush("wife", "a", "b", "c");
        System.out.println(jedis.lrange("wife", 0, -1));
        System.out.println(jedis.lpop("wife"));
        System.out.println(jedis.rpop("wife"));
        System.out.println(jedis.lrange("wife", 0, -1));
    4、集合类型(set)
        jedis.sadd("hasband", "kebi", "qingfeng", "wangsicong");
        Set<String> hasband = jedis.smembers("hasband");
        System.out.println(hasband);
    5、有序集合类型(sortedset)
        jedis.zadd("beautiful", 3, "huangtingting");
        jedis.zadd("beautiful", 2, "jujingyi");
        jedis.zadd("beautiful", 1, "mengmeiqi");
        Set<String> beautiful = jedis.zrange("beautiful", 0, -1);
        System.out.println(beautiful);
二、连接池
    1、使用
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(1000);
        jedisPoolConfig.setMaxIdle(100);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, "localhost", 6379);
        Jedis jedis = jedisPool.getResource();
        //操作数据类型
        jedis.close();
    2、配置
        #最大活动对象数
        redis.pool.maxTotal=1000
        #最大能够保持idle状态的对象数
        redis.pool.maxIdle=100
        #最小能够保持idle状态的对象数
        redis.pool.minIdle=50
        #当池内没有返回对象时,最大等待时间
        redis.pool.maxWaitMillis=10000
        #当调用borrow Object方法时,是否进行有效性检查
        redis.pool.testOnBorrow=true
        #当调用return Object方法时,是否进行有效性检查
        redis.pool.testOnReturn=true
        #“空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1.
        redis.pool.timeBetweenEvictionRunsMillis=30000
        #向调用者输出“链接”对象时,是否检测他的空闲超时
        redis.pool.testWhileIdle=true
        #对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3.
        redis.pool.numTestsPerEvictionRun=50
        #redis服务器的ip
        redis.ip=xxx.xxx.xxx.xxx
        #redis服务器的Port
        redis.port=6379
*/
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(1000);
        jedisPoolConfig.setMaxIdle(100);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, "localhost", 6379);
        Jedis jedis = jedisPool.getResource();
        //操作
        jedis.close();
    }
}

 

posted on 2020-09-03 17:04  一路繁花似锦绣前程  阅读(197)  评论(0编辑  收藏  举报