jedis 简单使用_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > jedis 简单使用

jedis 简单使用

 2015/3/19 1:00:12  antlove  程序员俱乐部  我要评论(0)
  • 摘要:jedis.RedisOperationCollection.javapackagejedis;importorg.apache.log4j.Logger;importredis.clients.jedis.Jedis;importjava.util.List;importjava.util.Map;importjava.util.Set;publicclassRedisOperationCollection{privatestaticLoggerlogger=Logger.getLogger
  • 标签:使用

jedis.RedisOperationCollection.java

class="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>

?

发表评论
用户名: 匿名