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>