欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jedis 简单使用 博客分类: cache javaredisjediscachecommand 

程序员文章站 2024-03-02 10:47:04
...

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>