下面使用window 安装redis
![]()
- 双击redis-server启动redis服务
- 双击redis-cli打开redis指令页面
![]()
使用Jedis操作redis
package redis;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Response;
import redis.clients.jedis.Transaction;
public class LocalhostRedisTest {
private static JedisPool pool;
static{
pool = new JedisPool();//空参默认本机 localhost 端口默认6379
}
private static Jedis getJedis(){
Jedis jedis=pool.getResource();
//jedis.auth("jms123456"); 本机不认证
return jedis;
}
public static void main(String[] args) {
Jedis jedis = getJedis();
jedis.del("orderinfo_json");
jedis.close();
Map<String, String> map=new HashMap<>();
map.put("1", "----------------------------");
map.put("2", "----------------------------");
map.put("3", "----------------------------");
map.put("4", "----------------------------");
map.put("5", "----------------------------");
map.put("6", "----------------------------");
map.put("7", "----------------------------");
map.put("8", "----------------------------");
hmset("orderinfo_json", map);
Set<String> keys=hkeys("orderinfo_json");
System.out.println(keys);
keys.remove("8");
keys.remove("7");
String[] keysArray=keys.toArray(new String[keys.size()]);
sadd("orderinfo_json_keys", keysArray);
jedis=getJedis();
jedis.watch("orderinfo_json");
//hmset("orderinfo_json", map);//有一个变化
List<String> values=hmget("orderinfo_json",keysArray);
System.out.println(values);
//redis事物
Transaction transaction=jedis.multi();//开启事物
transaction.hdel("orderinfo_json", "10","11");
List<Object> res=transaction.exec();//执行事物
jedis.close();//关闭连接
System.out.println(res);
System.out.println(res==null);
srem("orderinfo_json_keys", keysArray);
}
/**
* <p>通过key同时设置 hash的多个field</p>
* <p>此方法用的是hmset指令(hset单个向散列表添加数据,hmset批量添加)</p>
* @param key
* @param hash
* @return 返回OK 异常返回null
*/
public static String hmset(String key,Map<String, String> hash){
Jedis jedis = null;
String res = null;
try {
jedis = getJedis();
res = jedis.hmset(key, hash);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis!=null){
jedis.close();
}
}
return res;
}
/**
* <p>通过key删除set中对应的value值</p>
* @param key
* @param members 可以是一个String 也可以是一个String数组
* @return 删除的个数
*/
public static Long srem(String key,String...members){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.srem(key, members);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis!=null){
jedis.close();
}
}
return res;
}
/**
* <p>通过key 删除指定的 field </p>
* @param key
* @param fields 可以是 一个 field 也可以是 一个数组
* @return
*/
public static Long hdel(String key ,String...fields){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.hdel(key, fields);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis!=null){
jedis.close();
}
}
return res;
}
/**
* <p>通过key 和 fields 获取指定的value 如果没有对应的value则返回null</p>
* @param key
* @param fields 可以使 一个String 也可以是 String数组
* @return
*/
public static List<String> hmget(String key,String...fields){
Jedis jedis = null;
List<String> res = null;
try {
jedis = getJedis();
res = jedis.hmget(key, fields);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis!=null){
jedis.close();
}
}
return res;
}
public static Long sadd(String key,String...members){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.sadd(key, members);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis!=null){
jedis.close();
}
}
return res;
}
/**
* <p>通过key返回所有的field</p>
* @param key
* @return
*/
public static Set<String> hkeys(String key){
Jedis jedis = null;
Set<String> res = null;
try {
jedis = getJedis();
res = jedis.hkeys(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis!=null){
jedis.close();
}
}
return res;
}
public static Long rpush(String key ,String...strs){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.rpush(key, strs);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis!=null){
jedis.close();
}
}
return res;
}
/**
* <p>通过key保留list中从strat下标开始到end下标结束的value值</p>
* @param key
* @param start
* @param end
* @return 成功返回OK
*/
public static String ltrim(String key ,long start ,long end){
Jedis jedis = null;
String res = null;
try {
jedis = getJedis();
res = jedis.ltrim(key, start, end);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis!=null){
jedis.close();
}
}
return res;
}
/**
* <p>通过key返回list的长度</p>
* @param key
* @return
*/
public static Long llen(String key){
Jedis jedis = null;
Long res = null;
try {
jedis = getJedis();
res = jedis.llen(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis!=null){
jedis.close();
}
}
return res;
}
/**
* <p>通过key获取list指定下标位置的value</p>
* <p>如果start 为 0 end 为 -1 则返回全部的list中的value</p>
* @param key
* @param start
* @param end
* @return
*/
public static List<String> lrange(String key,long start,long end){
Jedis jedis = null;
List<String> res = null;
try {
jedis = getJedis();
res = jedis.lrange(key, start, end);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(jedis!=null){
jedis.close();
}
}
return res;
}
public static String hmsetAndValide(String key,String msetKey,Map<String, String> data){
Jedis jedis = null;
String res = null;
Transaction transaction=null;
try {
jedis = getJedis();
transaction=jedis.multi();
Response<Set<String>> response=transaction.smembers(key);
if(response!=null){
Set<String> strings=response.get();
if(strings!=null&&!strings.isEmpty()){
for (String string : strings) {
if(data.containsKey(string)){
return null;
}
}
}
}
Response<String> response2=transaction.hmset(key, data);
return response2.get();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(transaction!=null){
transaction.exec();
}
if(jedis!=null){
jedis.close();
}
}
return res;
}
}