import org.junit.Test;
import redis.clients.jedis.Jedis;
import java.util.*;
public class TestRedis {
private static Jedis jedis = new Jedis("localhost",6379);
@Test
public void setTest(){ //为指定的键设定值
String key = jedis.set("company","itcast");
System.out.println(key);
}
@Test
public void msetTest(){ //为多个键设定值,其中brand1,brand2,brand3是键
String manykey = jedis.mset("brand1","a","brand2","b","brand3","c");
System.out.println(manykey);
}
@Test
public void keysTest(){ //查看所有符合给定模式(正则表示下)的键
Set<String> keys = jedis.keys("*");
Iterator<String> iterator = keys.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
@Test
public void mgetTest(){ //获取多个键对应的值
List<String> mget = jedis.mget("brand1", "brand2");
for (String value:mget){
System.out.println(value);
}
}
@Test
public void existTest(){ //查看是否存在某键
Boolean brand1 = jedis.exists("brand1");
Boolean waad = jedis.exists("waad");
System.out.println(brand1+"-----------"+waad);
}
@Test
public void renameTest(){ //修改某键名称
String rename = jedis.rename("company", "company2");
System.out.println(rename);
}
@Test
public void delTest(){ //删除某个键
Long company2 = jedis.del("company2");
System.out.println(company2);
}
/*操作字符串*/
@Test
public void getsetTest(){ //获取指定键的旧值并且设定新值
String set = jedis.getSet("brand1", "itcast");
System.out.println(set);
}
@Test
public void strlenTest(){ //查看字符串键的值的长度
Long brand2 = jedis.strlen("brand2");
System.out.println(brand2);
}
@Test
public void getrangeTest(){ // 获取指定键字符串的指定索引范围的值
String brand1 = jedis.getrange("brand1", 2, 3);
System.out.println(brand1);
}
@Test
public void appendTest(){ //对指定键的字符串末尾追加新内容
Long append = jedis.append("brand1", "dwa");
System.out.println(append);
}
/*操作列表*/
@Test
public void rpushAndLpushTest(){ //将元素推到列表的左边和右边
Long rpush = jedis.rpush("color", "green", "pink");
Long lpush = jedis.lpush("eat", "orange");
System.out.println(rpush+"------------"+lpush);
}
@Test
public void lrangeTest(){ //获取列表指定索引范围内的元素
List<String> color = jedis.lrange("color", 0, 1);
for (String colors : color){
System.out.println(colors);
}
}
@Test
public void lindexTest(){ //获取列表指定索引上的元素
String color = jedis.lindex("color", 0);
System.out.println(color);
}
@Test
public void lpopTest(){ //获取列表最左端的元素
String color = jedis.lpop("color");
System.out.println(color);
}
@Test
public void llenTest(){ //获取列表长度
Long color = jedis.llen("color");
System.out.println(color);
}
@Test
public void lremTest(){ //移除列表中的指定元素
Long lrem = jedis.lrem("color", 1, "green");//移除掉从头开始搜索的第一个green的元素;
System.out.println(lrem);
}
/*操作集合*/
@Test
public void saddTest(){ //讲一个或者多个元素添加到集合中
Long sadd = jedis.sadd("database", "one", "two", "three");
System.out.println(sadd);
}
@Test
public void scardTest(){ //获取集合中元素数量
Long database = jedis.scard("database");
System.out.println(database);
}
@Test
public void smemberTest(){ //查看集合中的所有元素
Set<String> database = jedis.smembers("database");
Iterator<String> iterator = database.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
@Test
public void issmemberTest(){ //判断元素是否存在集合中
Boolean sismember = jedis.sismember("database", "redis");
System.out.println(sismember);
}
@Test
public void sremTest(){ // 移除集合一个或者多个已存在的元素
Long srem = jedis.srem("database", "one");
System.out.println(srem);
}
/*操作散列*/
@Test
public void hmsetTest(){ //为散列的多个键设置值
HashMap<String, String> map = new HashMap<>();
map.put("person","jack");
map.put("fruit","apple");
String article = jedis.hmset("article", map);
System.out.println(article);
}
@Test
public void hgetAllTest(){ // 获取散列中的所有键值对
Map<String, String> map = jedis.hgetAll("article");
Set<Map.Entry<String, String>> entries = map.entrySet();
Iterator<Map.Entry<String, String>> iterator = entries.iterator();
while (iterator.hasNext()){
Map.Entry<String, String> next = iterator.next();
System.out.println(next);
}
}
@Test
public void hkeysTest(){ //获取散列中所有键
Set<String> article = jedis.hkeys("article");
Iterator<String> iterator = article.iterator();
while (iterator.hasNext()){
String next = iterator.next();
System.out.println(next);
}
}
@Test
public void hvalsTest(){ //获取散列中所有的值
List<String> article = jedis.hvals("article");
for (String articles : article){
System.out.println(articles);
}
}
@Test
public void hdelTest(){ //删除散列中对应的键和值;
Long hdel = jedis.hdel("article", "fruit");
System.out.println(hdel);
}
/*操作有序集合*/
@Test
public void zaddTest(){ //有序集合添加一个或者多个键值对
Map<Double, String> map = new HashMap<>();
map.put(1.0,"old");
map.put(2.0,"young");
map.put(3.0,"middle");
Long ages = jedis.zadd("age",map);
System.out.println(ages);
}
@Test
public void zcountTest(){ //统计有序集合内指定分值范围内的元素个数
Long age = jedis.zcount("age", 1, 2);
System.out.println(age);
}
@Test
public void zrem(){ //移除有序集合中指定元素
Long zrem = jedis.zrem("age", "old");
System.out.println(zrem);
}
@Test
public void zrange(){ //查看指定索引范围的集合
Set<String> age = jedis.zrange("age", 0, 2);
for (String ages : age){
System.out.println(ages);
}
}
}
评论0