程序员编程点滴程序园

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

2019-05-01  本文已影响4人  夜风月圆

存储格式

基本用法

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
/*

上一篇下一篇

猜你喜欢

热点阅读