存儲(chǔ)格式
基本用法
Jedis工具[圖片上傳中...(image.png-8e2218-1556691362552-0)]
類(lèi)
redis配置文件
String
Hash
List
Set
SortedSet
存儲(chǔ)格式
基本用法
通過(guò)Jedis(封裝了redis的Java客戶(hù)端)對(duì)redis進(jìn)行操作啦粹。
Jedis工具類(lèi)
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();
//最大連接數(shù)
poolConfig.setMaxTotal(Integer.parseInt( pro.get("redis.maxTotal").toString()));
//最大空閑連接數(shù)
poolConfig.setMaxIdle(Integer.parseInt( pro.get("redis.maxIdle").toString()));
//最小空閑連接數(shù)
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
//加上/減去 一個(gè)數(shù)
//incrBy 返回的是修改之后的值如果原值是字符串不是數(shù)字妇垢,則會(huì)拋出異常
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對(duì)象
// 適合存儲(chǔ)鍵值對(duì)象的信息
@Test
//存值 參數(shù)第一個(gè)變量的名稱(chēng), map鍵名(key), map鍵值(value)
// 調(diào)用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
//也可以存多個(gè)key
// 調(diào)用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中的鍵 可以刪除一個(gè)也可以刪除多個(gè)掌测,返回的是刪除的個(gè)數(shù)
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中的鍵值對(duì)
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是否存在某個(gè)值
public void fun5() {
System.out.println(jedis.hexists("hash2", "username"));
System.out.println(jedis.hexists("hash2", "age"));
}
@Test
//獲取hash中鍵值對(duì)的個(gè)數(shù)
public void fun6() {
System.out.println(jedis.hlen("hash2"));
}
// 獲取一個(gè)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("===========添加一個(gè)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的內(nèi)容:"+jedis.lrange("collections", 0, -1));//-1代表倒數(shù)第一個(gè)元素寡壮,-2代表倒數(shù)第二個(gè)元素
System.out.println("collections區(qū)間0-3的元素:"+jedis.lrange("collections",0,3));
System.out.println("===============================");
// 刪除列表指定的值 衫生,第二個(gè)參數(shù)為刪除的個(gè)數(shù)(有重復(fù)時(shí))凹炸,后add進(jìn)去的值先被刪采桃,類(lèi)似于出棧
System.out.println("刪除指定元素個(gè)數(shù):"+jedis.lrem("collections", 2, "HashMap"));
System.out.println("collections的內(nèi)容:"+jedis.lrange("collections", 0, -1));
System.out.println("刪除下表0-3區(qū)間之外的元素:"+jedis.ltrim("collections", 0, 3));
System.out.println("collections的內(nèi)容:"+jedis.lrange("collections", 0, -1));
System.out.println("collections列表出棧(左端):"+jedis.lpop("collections"));
System.out.println("collections的內(nèi)容:"+jedis.lrange("collections", 0, -1));
System.out.println("collections添加元素懒熙,從列表右端,與lpush相對(duì)應(yīng):"+jedis.rpush("collections", "EnumMap"));
System.out.println("collections的內(nèi)容:"+jedis.lrange("collections", 0, -1));
System.out.println("collections列表出棧(右端):"+jedis.rpop("collections"));
System.out.println("collections的內(nèi)容:"+jedis.lrange("collections", 0, -1));
System.out.println("修改collections指定下標(biāo)1的內(nèi)容:"+jedis.lset("collections", 1, "LinkedArrayList"));
System.out.println("collections的內(nèi)容:"+jedis.lrange("collections", 0, -1));
System.out.println("===============================");
System.out.println("collections的長(zhǎng)度:"+jedis.llen("collections"));
System.out.println("獲取collections下標(biāo)為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類(lèi)的區(qū)別就是
set中不會(huì)出現(xiàn)重復(fù)的數(shù)據(jù)
他可以進(jìn)行聚合操作效率比較高
其余的操作基本上和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
//判斷是否這個(gè)set中存在某個(gè)值
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
// 成員數(shù)量
public void fun7(){
System.out.println(jedis.scard("myset1"));
}
@Test
// 獲得隨機(jī)的一個(gè)成員
public void fun8(){
System.out.println(jedis.srandmember("myset1"));
}
@Test
// 將相差的成員放到一個(gè)新的set中同理交集和并集都可以后面均
// 加上一個(gè)store即可
// 并返回新的長(zhǎng)度
public void fun9(){
System.out.println(jedis.sdiffstore("myset3","myset1","myset2"));
System.out.println(jedis.smembers("myset3"));
}
}
SortedSet
/*
和set極為的類(lèi)似工扎,他們是字符串的集合,沒(méi)有重復(fù)的數(shù)據(jù)
差別是sortedset每個(gè)成員中都會(huì)有一個(gè)分?jǐn)?shù)(score)與之關(guān)聯(lián)
衔蹲,redis正是通過(guò)分?jǐn)?shù)來(lái)為集合中的成員進(jìn)行從小到大的排序
sortedset中數(shù)據(jù)必須單一但是他的score可以是重復(fù)的
*/
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);
}
}