1、两种方式安装:
2、redis基础使用
3、通过jmeter-Redis Datta Set的方式获取List或者Set的值
3.1、安装插件-Redis Data Set
3.2、Redis Data Set-List 获取List中的值
3.3、Redis Data Set- Set 获取Set中的值
4、通过jmeter-BeanShell Sampler的方式获取List、Set、String、Zset、hash的值
4.1、String
import redis.clients.jedis.Jedis;
import java.util.ArrayList;
import org.apache.commons.lang3.StringUtils;
String host = "127.0.0.1"; //服务器地址
int port = 6379; //端口号
String password = ""; //redis密码
int index = 0; //redis db,
String key = "db0"; //key值
Jedis jedis = new Jedis(host, port);
if(StringUtils.isNotBlank(password)){
jedis.auth(password);
}
//add key value
/*jedis.set(String key, String value) //存储value到变量key*/
jedis.set("db0","${data}");
log.info("add key value");
//get key value
jedis.select(index);
String redisData= jedis.get(key);
log.info("get key value is:"+ redisData);
/*vars.put(String key, String value) //存储value到变量key,并且传给jmeter*/
vars.put("redisData",redisData);
//del key value
delData = jedis.del("db0");
log.info("del key value:"+ delData);
log.info("del key value:"+ delData);
log.info("del key value:"+ delData);
log.info("del key value:"+ delData);
log.info("del key value:"+ delData);
//get key value 检查是否删除成功
String redisData= jedis.get(key);
log.info("get key value is:"+ redisData);
//close jedis
jedis.close();
log.info("close jedis");
4.2、List
import redis.clients.jedis.Jedis;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.lang.reflect.Method;
String host = "127.0.0.1"; //服务器地址
int port = 6379; //端口号
String password = ""; //redis密码
int index = 0; //redis db,
String key = "ages"; //key值
Jedis jedis = new Jedis(host, port);
if(StringUtils.isNotBlank(password))
{
jedis.auth(password);
}
//清空数据
//jedis.flushDB();
//log.info("---------redis info-------------------" + jedis.info());
//add key value
/*jedis.Ipush( String key, new String[]{"value1", "value2", "value3"}) //将多个值存放到集合中*/
jedis.lpush(key, new String[]{"xx", "yy", "zz"});
log.info("add values");
//get key value
List value = jedis.lrange(key,0,-1);
log.info("get values:"+ value);
String str = value.toString();
/*vars.put(String key, String value) //存储value到变量key,并且传给jmeter*/
vars.put("redisData",str);
//del key value
jedis.del(key);
//close jedis
jedis.close();
log.info("close jedis");
//判断方法是否可用
/*for (Method method : jedis.getClass().getMethods()) {
if (method.toGenericString().contains("lpush")) {
log.info(method.toGenericString());
}
}
*/
4.3、Hash
import redis.clients.jedis.Jedis;
import org.apache.commons.lang3.StringUtils;
import java.util.Map;
//import java.util.List;
//import java.util.ArrayList;
//redis服务器地址
String host = "127.0.0.1";
//redis 服务端口号
int port = 6379;
//redis密码
String password ="" ;
//redis db索引默认从0开始
int index = 0;
String key ="test";
//Jedis获取到的Redis数据在jedis里
Jedis jedis = new Jedis(host, port);
//判断密码是否为空,不为空时校验密码
if(StringUtils.isNotBlank(password))
{
jedis.auth(password);
}
//新增哈希key为test对应的值,括号中分别为key,field,value
addhash = jedis.hset(key,"id","5");
log.info("sethash is :" +addhash);
//获取哈希key对应的值
Map gethash = jedis.hgetAll(key);
log.info("gethash is :" + gethash);
log.info("gethash kyes is :" + gethash.keySet());
log.info("gethash values is :" + gethash.values());
log.info("gethash id's values is:"+ gethash.get("id"));
//str中存放全部数据
String str = gethash.get("id");
//将key的值保存为变量,传给jmeter
vars.put("redisData",str);
log.info("redisData is:"+ gethash.get("id"));
//删除哈希key为test的值
delhash = jedis.del(key);
log.info("---------delhash is--------- :" + delhash);
//检查是否删除成功
String str = gethash.get("id");
log.info("redisData is:"+ str);
//关闭jedis
jedis.close();
log.info("close jedis");
4.4、Set
import redis.clients.jedis.Jedis;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.lang.reflect.Method;
String host = "127.0.0.1"; //服务器地址
int port = 6379; //端口号
String password = ""; //redis密码
int index = 0; //redis db,
String key = "colour"; //key值
Jedis jedis = new Jedis(host, port);
if(StringUtils.isNotBlank(password))
{
jedis.auth(password);
}
//清空数据
jedis.flushDB();
//log.info("---------redis info-------------------" + jedis.info());
/*jedis.sadd( String key, new String[]{"value1", "value2", "value3"}) //将字符串类型的数组存放到集合中*/
//add key value
jedis.sadd(key,new String[]{"red", "yellow", "blue"});
log.info("add values");
//get key value
Set value = jedis.smembers(key);
log.info("get values:"+ value);
String str = value.toString();
/*vars.put(String key, String value) //存储value到变量key,并且传给jmeter*/
vars.put("redisData",str);
//del key value
jedis.del(key);
//close jedis
jedis.close();
log.info("close jedis");
////判断方法是否可用
///*for (Method method : jedis.getClass().getMethods()) {
// if (method.toGenericString().contains("lpush")) {
// log.info(method.toGenericString());
// }
// }
//*/
//
4.5、Zset
import redis.clients.jedis.Jedis;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.lang.reflect.Method;
String host = "127.0.0.1"; //服务器地址
int port = 6379; //端口号
String password = ""; //redis密码
int index = 0; //redis db,
String key = "score"; //key值
Jedis jedis = new Jedis(host, port);
if(StringUtils.isNotBlank(password))
{
jedis.auth(password);
}
//清空数据
jedis.flushDB();
//log.info("---------redis info-------------------" + jedis.info());
//add key value
/*jedis.Ipush( String key, Double value, String value) //将值存放到集合中*/
jedis.zadd(key, 100.00, "yuwen");
jedis.zadd(key, 90.00, "math");
jedis.zadd(key, 80.00, "art");
jedis.zadd(key, 70.00, "sport");
jedis.zadd(key, 60.00, "science");
log.info("add values");
////get key value
Set value = jedis.zrange(key, 0, 10);
log.info("get values:"+ value);
String str = value.toString();
/*vars.put(String key, String value) //存储value到变量key,并且传给jmeter*/
vars.put("redisData",str);
//del key value
jedis.del(key)
//close jedis
jedis.close();
log.info("close jedis");
//
////判断方法是否可用
///*for (Method method : jedis.getClass().getMethods()) {
// if (method.toGenericString().contains("lpush")) {
// log.info(method.toGenericString());
// }
// }
//*/
//
5、使用redis命令行删除key
连接redis
```redis-cli```
使用redis
-h 域名
-p 端口
-a 密码
-n 选择数据库,不写默认是DB0
keys 过滤以db0为前缀的key
xargs 捕获一个命令的输出,然后传递给另外一个命令。
删掉单个key:
```redis-cli -h 127.0.0.1 -p 6379 -n 0 keys "db0" | xargs redis-cli -h 127.0.0.1 -p 6379 -n 0 del```
批量删除key,以分号结尾:
```redis-cli -h 127.0.0.1 -p 6379 -n 0 keys "db0*" | xargs redis-cli -h 127.0.0.1 -p 6379 -n 0 del;```