当前位置:首页 > 开发 > 互联网 > 正文

jedis 简单使用

发表于: 2015-03-18   作者:antlove   来源:转载   浏览:
摘要: jedis.RedisOperationCollection.java package jedis; import org.apache.log4j.Logger; import redis.clients.jedis.Jedis; import java.util.List; import java.util.Map; import java.util.Set; pub

jedis.RedisOperationCollection.java

package jedis;

import org.apache.log4j.Logger;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class RedisOperationCollection {

    private static Logger logger = Logger.getLogger(RedisOperationCollection.class);

    private static Jedis jedis = new Jedis("127.0.0.1");

    private static void showCommonOperations(){
        Set<String> keys = jedis.keys("*");// select all the keys
        logger.info("all keys in this db are : "+keys);

        boolean keyExisted = jedis.exists("key");// check whether the specified key is existed
        logger.info("key existed : "+keyExisted);

        String keyType = jedis.type("list");// check key type
        logger.info("key type : "+keyType);
    }

    private static void showListOperations(){
        jedis.del("list");
        jedis.lpush("list","abc");
        jedis.rpush("list", "hij");
        List<String> list = jedis.lrange("list",0,-1);
        logger.info("list information is  : "+list);
    }

    private static void showHashOperations(){
        jedis.hset("hash","name","姓名");
        Map<String, String> hash = jedis.hgetAll("hash");
        logger.info("the hash information is "+hash);

    }

    private static void showSetOperations(){
        jedis.sadd("set","a","b","c");
        jedis.spop("set");
        jedis.srem("set","b");
        Set<String> set = jedis.smembers("set");
        logger.info("the set information is "+set);
    }

    private static void showSortedSetOperations(){
        jedis.zadd("zset",0,"java");
        jedis.zadd("zset",1,"C");
        jedis.zadd("zset",2,"C++");
        jedis.zadd("zset",3,"C#");
        Set<String> zset = jedis.zrange("zset", 0, -1);
        logger.info("the zset information is "+zset);
    }

    public static void main(String[] args) {
        showCommonOperations();
        showListOperations();
        showHashOperations();
        showSetOperations();
        showSortedSetOperations();
    }



}

 

pom.xml

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.4.2</version>
        </dependency>

 

jedis 简单使用

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号