Java 中 Redis 五大基本类型的用法

96
夜风月圆
2019.05.01 14:16 字数 1502

存储格式

基本用法

Jedis工具[图片上传中...(image.png-8e2218-1556691362552-0)]

redis配置文件

String

Hash

List

Set

SortedSet
存储格式


image.png

基本用法
通过Jedis(封装了redis的Java客户端)对redis进行操作。

Jedis工具类
public class JedisPoolUtil {
private static JedisPool pool = null;
static {
//加载配置文件
InputStream in = JedisPoolUtil.class.getClassLoader().getResourceAsStream("redis.properties");
Properties pro = new Properties();
try {
pro.load(in);
} catch (IOException e) {
e.printStackTrace();
System.out.println("加载文件失败");
}
JedisPoolConfig poolConfig = new JedisPoolConfig();
//最大连接数
poolConfig.setMaxTotal(Integer.parseInt( pro.get("redis.maxTotal").toString()));
//最大空闲连接数
poolConfig.setMaxIdle(Integer.parseInt( pro.get("redis.maxIdle").toString()));
//最小空闲连接数
poolConfig.setMinIdle(Integer.parseInt( pro.get("redis.minIdle").toString()));
pool = new JedisPool(poolConfig, pro.get("redis.url").toString(),Integer.parseInt( pro.get("redis.port")
.toString()));
}

public static Jedis getJedis(){
    return pool.getResource();
}
public static void release(Jedis jedis){
    if(null != jedis){
        jedis.close();
    }
}

}
redis配置文件
redis.maxTotal=100
redis.maxIdle=30
redis.minIdle=10
redis.url=192.168.202.200
redis.port=6379
String
public class StringTest {
public Jedis jedis = JedisPoolUtil.getJedis();
@Test
//添加和获取
public void fun(){
jedis.set("num","1");
System.out.println(jedis.get("num"));
}
@Test
//删除值
public void fun1(){
jedis.del("num");
System.out.println(jedis.get("num"));
}
@Test
//自减和自减
public void fun2(){
jedis.set("num","1");
System.out.println(jedis.get("num"));
jedis.decr("num");
System.out.println(jedis.get("num"));
jedis.incr("num");
jedis.incr("num");
System.out.println(jedis.get("num"));
}
@Test
//加上/减去 一个数
//incrBy 返回的是修改之后的值如果原值是字符串不是数字,则会抛出异常
public void fun3(){
Long num = jedis.incrBy("num", 3);
System.out.println(num);
jedis.decrBy("num",10);

    System.out.println(jedis.get("num"));
    jedis.set("name","caopengfei");
    //jedis.decrBy("name",1);
}
@Test
//字符串拼接
public void fun4(){
    Long len = jedis.append("name", "123");
    System.out.println(len);
    System.out.println(jedis.get("name"));
}

}
Hash
public class HashTest {
public Jedis jedis = JedisPoolUtil.getJedis();

//    hash 操作的是map对象

// 适合存储键值对象的信息
@Test
//存值 参数第一个变量的名称, map键名(key), map键值(value)
// 调用hset
public void fun() {
Long num = jedis.hset("hash1", "username", "caopengfei");
System.out.println(num);
String hget = jedis.hget("hash1", "username");
System.out.println(hget);
}

@Test
//也可以存多个key

// 调用hmset
public void fun1() {
Map<String, String> map = new HashMap<String, String>();
map.put("username", "caopengfei");
map.put("age", "25");
map.put("sex", "男");
String res = jedis.hmset("hash2", map);
System.out.println(res);//ok
}

@Test
//获取hash中所有的值
public void fun2() {
    Map<String, String> map2 = new HashMap<String, String>();
    map2 = jedis.hgetAll("hash2");
    System.out.println(map2);
}

@Test

// 删除hash中的键 可以删除一个也可以删除多个,返回的是删除的个数
public void fun3() {
Long num = jedis.hdel("hash2", "username", "age");
System.out.println(num);
Map<String, String> map2 = new HashMap<String, String>();
map2 = jedis.hgetAll("hash2");
System.out.println(map2);
}

@Test
//增加hash中的键值对
public void fun4() {
    Map<String, String> map2 = new HashMap<String, String>();
    map2 = jedis.hgetAll("hash2");
    System.out.println(map2);
    jedis.hincrBy("hash2", "age", 10);
    map2 = jedis.hgetAll("hash2");
    System.out.println(map2);
}

@Test
//判断hash是否存在某个值
public void fun5() {
    System.out.println(jedis.hexists("hash2", "username"));
    System.out.println(jedis.hexists("hash2", "age"));
}

@Test
//获取hash中键值对的个数
public void fun6() {
    System.out.println(jedis.hlen("hash2"));
}

//    获取一个hash中所有的key值
@Test
public void fun7() {
    Set<String> hash2 = jedis.hkeys("hash2");
    System.out.println(hash2);
}

//    获取所有的value值
@Test
public void fun8() {
    List<String> hash2 = jedis.hvals("hash2");
    System.out.println(hash2);
}

}
List
public void testList()
{
jedis.flushDB();
System.out.println("===========添加一个list===========");
jedis.lpush("collections", "ArrayList", "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap");
jedis.lpush("collections", "HashSet");
jedis.lpush("collections", "TreeSet");
jedis.lpush("collections", "TreeMap");
System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1));//-1代表倒数第一个元素,-2代表倒数第二个元素
System.out.println("collections区间0-3的元素:"+jedis.lrange("collections",0,3));
System.out.println("===============================");
// 删除列表指定的值 ,第二个参数为删除的个数(有重复时),后add进去的值先被删,类似于出栈
System.out.println("删除指定元素个数:"+jedis.lrem("collections", 2, "HashMap"));
System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1));
System.out.println("删除下表0-3区间之外的元素:"+jedis.ltrim("collections", 0, 3));
System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1));
System.out.println("collections列表出栈(左端):"+jedis.lpop("collections"));
System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1));
System.out.println("collections添加元素,从列表右端,与lpush相对应:"+jedis.rpush("collections", "EnumMap"));
System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1));
System.out.println("collections列表出栈(右端):"+jedis.rpop("collections"));
System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1));
System.out.println("修改collections指定下标1的内容:"+jedis.lset("collections", 1, "LinkedArrayList"));
System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1));
System.out.println("===============================");
System.out.println("collections的长度:"+jedis.llen("collections"));
System.out.println("获取collections下标为2的元素:"+jedis.lindex("collections", 2));
System.out.println("===============================");
jedis.lpush("sortedList", "3","6","2","0","7","4");
System.out.println("sortedList排序前:"+jedis.lrange("sortedList", 0, -1));
System.out.println(jedis.sort("sortedList"));
System.out.println("sortedList排序后:"+jedis.lrange("sortedList", 0, -1));
}
Set
/*

  • Set集合,和List类的区别就是

  • set中不会出现重复的数据

  • 他可以进行聚合操作效率比较高

  • 其余的操作基本上和list相同

  • */
    public class SetTest {
    public Jedis jedis = JedisPoolUtil.getJedis();

    @Test
    /添加元素删除元素/
    public void fun(){
    Long num = jedis.sadd("myset", "a", "a", "b","abc");
    System.out.println(num);

    }
    @Test
    /获得元素/
    public void fun1(){
    Set<String> myset = jedis.smembers("myset");
    System.out.println(myset);
    }
    @Test
    /移除元素/
    public void fun2(){
    jedis.srem("myset","a","b");
    Set<String> myset = jedis.smembers("myset");
    System.out.println(myset);
    }
    @Test
    //判断是否这个set中存在某个值
    public void fun3(){
    Boolean sismember = jedis.sismember("myset", "a");
    System.out.println(sismember);
    }
    @Test
    //获得A-B 获得差集合
    public void fun4(){
    jedis.sadd("myset1","123","32","abc","def","123456","sdfasd");
    jedis.sadd("myset2","abc","345","123","fda");
    Set<String> sdiff = jedis.sdiff("myset1", "myset2");
    System.out.println(sdiff);
    }
    @Test
    //获得交集
    public void fun5(){
    Set<String> sinter = jedis.sinter("myset1", "myset2");
    System.out.println(sinter);

    }
    @Test
    // 获得并集合
    public void fun6(){
    Set<String> sunion = jedis.sunion("myset1", "myset2");
    System.out.println(sunion);
    }
    @Test
    // 成员数量
    public void fun7(){
    System.out.println(jedis.scard("myset1"));
    }
    @Test
    // 获得随机的一个成员
    public void fun8(){
    System.out.println(jedis.srandmember("myset1"));
    }
    @Test
    // 将相差的成员放到一个新的set中同理交集和并集都可以后面均
    // 加上一个store即可
    // 并返回新的长度
    public void fun9(){
    System.out.println(jedis.sdiffstore("myset3","myset1","myset2"));
    System.out.println(jedis.smembers("myset3"));
    }
    }
    SortedSet
    /*
    和set极为的类似,他们是字符串的集合,没有重复的数据
    差别是sortedset每个成员中都会有一个分数(score)与之关联
    ,redis正是通过分数来为集合中的成员进行从小到大的排序
    sortedset中数据必须单一但是他的score可以是重复的
    */
    public class SortedsetTest {
    public Jedis jedis = JedisPoolUtil.getJedis();
    // 添加元素
    @Test
    public void fun(){
    jedis.zadd("mysort",100.0, "zhangsan");
    jedis.zadd("mysort",200.0,"lisi");
    jedis.zadd("mysort",50.0,"wangwu");
    Map<String ,Double>map = new HashMap<String ,Double>();
    map.put("mutouliu",70.0);

      jedis.zadd("mysort",map);
      Set<String> mysort = jedis.zrange("mysort", 0, -1);
      System.out.println(mysort);
      Set<String> mysort1 = jedis.zrange("mysort", 1, 2);
      System.out.println(mysort1);
    

    }
    }

日记本