redis 下载及使用

redis 官网下载地址:http://redis.io/

redis 下载及使用

E:\工作软件\新建文件夹\redis64-2.8.19  redis-server.exe 执行该命令

redis 下载及使用

当前已启动  端口号:6379

redis.conf 该配制文件中配置登陆密码

在次下面配置################################## SECURITY ###################################

# Require clients to issue AUTH <PASSWORD> before processing any other
# commands. This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
requirepass "123456"

redis 可视化工具RedisStudio下载地址 :https://github.com/cinience/RedisStudio/releases  下载之后直接打开

redis 下载及使用

填写完毕之后直接save就可以了

在此下面查找数据

redis 下载及使用

redis java的增删改查:

/**
* 根据key删除value
* @param key
*/
public void remove(String key){
if(key == null || "".equals(key)){
throw new ServiceException("key值不能为空!");
}
JedisPool pool = null;
Jedis jedis = null;
try{
pool = getPool();
jedis = pool.getResource();
if(jedis.exists(key)){
jedis.del(key);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(jedis != null){
jedis.close();
}
}
} /**
* 根据key读取value值,可根据传入类型进行转型
* @param key
* @param clazz
* @param <T>
* @return
*/
public <T> T getData(String key, Class<T> clazz){
if(key == null || "".equals(key)){
throw new ServiceException("key值不能为空!");
}
Object result = null;
JedisPool pool = null;
Jedis jedis = null;
try{
pool = getPool();
jedis = pool.getResource();
result = jedis.get(key);
}catch (Exception e){
e.printStackTrace();
}finally {
if(jedis != null){
jedis.close();
}
}
JsonMapper m = new JsonMapper();
return (T) m.fromJson(String.valueOf(result), clazz);
} /**
* 新增/修改,需要传入key,value可传入对象,生存时间(单位:秒)
* @param key
* @param obj
* @param t
*/
public void save(String key, Object obj, int t){
if(key == null || "".equals(key)){
throw new ServiceException("key值不能为空!");
} if(obj == null){
throw new ServiceException("value值不能为空!");
} JsonMapper m = new JsonMapper();
String val = m.toJson(obj);
logger.debug("新增key=>" + key + "及value=>" + val);
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
jedis.set(key, val);
jedis.expire(key, t);
}catch (Exception e){
e.printStackTrace();
throw new ServiceException("保存redis数据失败!");
}finally {
if(jedis != null){
jedis.close();
}
}
} /**
* 更新key的生存时间(单位:秒)
* @param key
* @param t
* @return
*/
public Long expireRow(String key, int t){
if(key == null || "".equals(key)){
throw new ServiceException("key值不能为空!");
} Long res = 0L;
JedisPool pool = null;
Jedis jedis = null;
try{
pool = getPool();
jedis = pool.getResource();
res = jedis.expire(key, t);
}catch (Exception e){
e.printStackTrace();
}finally {
if(jedis != null){
jedis.close();
}
}
return res;
} public static Map<String, Object> beanToMap(Object obj) {
if(obj == null){
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName(); // 过滤class属性
if (!key.equals("class")) {
// 得到property对应的getter方法
Method getter = property.getReadMethod();
Object value = getter.invoke(obj); map.put(key, value);
}
}
} catch (Exception e) {
System.out.println("transBean2Map Error " + e);
}
return map;
}
上一篇:《Entity Framework 6 Recipes》中文翻译系列 (21) -----第四章 ASP.NET MVC中使用实体框架之在页面中创建查询和使用ASP.NET URL路由过虑


下一篇:ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第三章:搜索、高级过滤和视图模型