Redis hash是一個string類型的field和value的映射表.一個key可對應多個field,一個field對應一個valuejava
一、如何主數據在redis緩存中實現初始化?web
1)加載主數據,創建MasterMain.javaredis
package com.taikang.healthcare.master.service;spring
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;數據庫
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;緩存
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;app
import com.taikang.healthcare.master.dao.BaseCodeMapper;
import com.taikang.healthcare.master.dao.RedisDao;
import com.taikang.healthcare.master.model.BaseCode;
import com.taikang.healthcare.sdk.BeanUtil;ide
public class MasterMain implements ApplicationListener<ContextRefreshedEvent> {
@Resource
private BaseCodeMapper baseCodeMapper;
@Resource
RedisDao redisDao;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if(event.getApplicationContext().getParent() == null){//root application context 沒有parent,他就是老大.spa
//須要執行的邏輯代碼,當spring容器初始化完成後就會執行該方法。
//定義一個List用於接收轉換後的查詢結果
List<Map<String, Object>> baseCodeList = new ArrayList<Map<String, Object>>();
//根據傳進來的任意字段進行查詢
List<BaseCode> baseCodes = baseCodeMapper.selectByAnyField(null);
//將查詢結果遍歷
for(BaseCode baseCode :baseCodes){
//將查詢結果初始化到redis
String ObjectId=baseCode.getObjectId().toString();
String Id=baseCode.getId().toString();
String Name=baseCode.getName();
System.out.println(ObjectId);
System.out.println(Id);
System.out.println(Name);
redisDao.hset("basecode_"+ObjectId, Id, Name) ;
}.net
}
}
}
2)啓動初始化
在master項目中的applicationContext.xml中增長,啓動時調用初始化類加載主數據
<!-- 當Spring容器啓動完成後執行下面的這個主數據緩存初始化 -->
<bean class="com.taikang.healthcare.master.service.MasterMain"/>
二、如何實現主數據在redis中的增刪改查?
hash數據操做:
1) 使用步驟:
(1)把文件RedisDao由於項目的Dao層中
(2)引入
@Resource
RedisDao redisDao;
(3)書寫方式
根據獲取 redisDao.isopen()爲0進入直接在數據庫中查詢主數據;爲1則在緩存中獲取主數據信息。
System.out.println("======================hash==========================");
//清空數據
System.out.println(jedis.flushDB());
System.out.println("=============增=============");
System.out.println("hashs中添加key001和value001鍵值對:"+redisDao.hset("hashs", "key001", "test"));
System.out.println("hashs中添加key002和value002鍵值對:"+redisDao.hset("hashs", "key002", "value002"));
System.out.println("hashs中添加key003和value003鍵值對:"+redisDao.hset("hashs", "key003", "value003"));
System.out.println("新增key004和4的整型鍵值對:"+redisDao.hincrBy("hashs", "key004", 4l));
System.out.println("hashs中的全部值:"+redisDao.hvals("hashs"));
System.out.println("=============刪=============");
System.out.println("hashs中刪除key002鍵值對:"+redisDao.hdel("hashs", "key002"));
System.out.println("hashs中的全部值:"+redisDao.hvals("hashs"));
System.out.println("=============改=============");
System.out.println("key004整型鍵值的值增長100:"+redisDao.hincrBy("hashs", "key004", 100l));
System.out.println("直接覆蓋key001原來的數據:"+redisDao.hset("hashs","key001","test"));
System.out.println("hashs中的全部值:"+redisDao.hvals("hashs"));
System.out.println("=============查=============");
System.out.println("判斷key003是否存在:"+redisDao.hexists("hashs", "key003"));
System.out.println("獲取key004對應的值:"+redisDao.hget("hashs", "key004"));
System.out.println("批量獲取key001和key003對應的值:"+redisDao.hmget("hashs", "key001", "key003"));
System.out.println("獲取hashs中全部的key:"+redisDao.hkeys("hashs"));
System.out.println("獲取hashs中全部的value:"+redisDao.hvals("hashs"));
System.out.println("獲取hashs中全部的field,value:"+redisDao.hgetAll("hashs"));
2)使用說明:
redis開關配置:
//是否開啓redis緩存 0關閉1開啓
public int isopen() {
//默認爲redis關閉
int value = 0;
return value;
}
目前裝載可調用的方法爲:
/**
*
* @Title : hset
* @Description: 將哈希表key中的域field的值設爲value。若是key不存在,一個新的哈希表被建立並進行hset操做。若是域field已經存在於哈希表中,舊值將被覆蓋。
* @param @param key
* @param @param field
* @param @param value 設定文件
* @return void 返回類型
* @throws
*/
public void hset(String key,String field,String value){
ShardedJedis jedis = null;
try {
jedis = shardedJedisPool.getResource();
jedis.hset(key,field, value);
} catch (RuntimeException e) {
if (jedis != null) {
shardedJedisPool.returnBrokenResource(jedis);
}
e.printStackTrace();
} finally {
// 正確釋放資源
if (jedis != null) {
shardedJedisPool.returnResource(jedis);
}
}
}
/**
*
* @Title: hincrBy
* @Description: 爲哈希表key中的域field的值加上增量increment。增量也能夠爲負數,至關於對給定域進行減法操做。
* @param @param key
* @param @param field
* @param @param value 設定文件
* @return void 返回類型
* @throws
*/
public void hincrBy(String key,String field,long value){
ShardedJedis jedis = null;
try {
jedis = shardedJedisPool.getResource();
jedis.hincrBy(key,field, value);
} catch (RuntimeException e) {
if (jedis != null) {
shardedJedisPool.returnBrokenResource(jedis);
}
e.printStackTrace();
} finally {
// 正確釋放資源
if (jedis != null) {
shardedJedisPool.returnResource(jedis);
}
}
}
/**
*
* @Title: hdel
* @Description: 刪除哈希表key中的一個或多個指定域,不存在的域將被忽略。
* @param @param key
* @param @param fields
* @param @return 設定文件
* @return boolean 返回類型
* @throws
*/
public boolean hdel(String key, String... fields) {
log.info("redis--------刪除-----------key-----健值對:" + key);
ShardedJedis jedis = null;
try {
jedis = shardedJedisPool.getResource();
jedis.hdel(key, fields);
return true;
} catch (RuntimeException e) {
if (jedis != null) {
shardedJedisPool.returnBrokenResource(jedis);
}
e.printStackTrace();
} finally {
// 正確釋放資源
if (jedis != null) {
shardedJedisPool.returnResource(jedis);
}
}
return false;
}
/**
*
* @Title: hget
* @Description: 返回哈希表key中指定的field的值。
* @param @param key
* @param @param field
* @param @return 設定文件
* @return String 返回類型
* @throws
*/
public String hget(String key, String field) {
log.info("redis--------獲取-----------key-----健:" + key);
ShardedJedis jedis = null;
String value = "";
try {
jedis = shardedJedisPool.getResource();
value = jedis.hget(key, field);
} catch (RuntimeException e) {
if (jedis != null) {
shardedJedisPool.returnBrokenResource(jedis);
}
e.printStackTrace();
} finally {
// 正確釋放資源
if (jedis != null) {
shardedJedisPool.returnResource(jedis);
}
}
log.info("redis--------獲取-----------key-----健:" + key);
return value;
}
/**
*
* @Title: hexists
* @Description: 查看哈希表key中,給定域field是否存在
* @param @param key
* @param @param field
* @param @return 設定文件
* @return boolean 返回類型
* @throws
*/
public boolean hexists(String key, String field) {
log.info("redis--------判斷是否存在:" + key);
ShardedJedis jedis = null;
try {
jedis = shardedJedisPool.getResource();
jedis.hexists(key, field);
return true;
} catch (RuntimeException e) {
if (jedis != null) {
shardedJedisPool.returnBrokenResource(jedis);
}
e.printStackTrace();
} finally {
// 正確釋放資源
if (jedis != null) {
shardedJedisPool.returnResource(jedis);
}
}
return false;
}
/**
*
* @Title: hmget
* @Description: 返回哈希表key中,一個或多個給定域的值。
* @param @param key
* @param @param fields
* @param @return 設定文件
* @return List<String> 返回類型
* @throws
*/
public List<String> hmget(String key, String... fields) {
log.info("redis--------批量獲取-----------key-----健:" + key);
ShardedJedis jedis = null;
List<String> value = new ArrayList<String>();
try {
jedis = shardedJedisPool.getResource();
value = jedis.hmget(key, fields);
} catch (RuntimeException e) {
if (jedis != null) {
shardedJedisPool.returnBrokenResource(jedis);
}
e.printStackTrace();
} finally {
// 正確釋放資源
if (jedis != null) {
shardedJedisPool.returnResource(jedis);
}
}
log.info("redis--------批量獲取-----------key-----健:" + key);
return value;
}
/**
*
* @Title: hkeys
* @Description: 得到哈希表中key對應的全部field。
* @param @param key
* @param @return 設定文件
* @return Set<String> 返回類型
* @throws
*/
public Set<String> hkeys(String key) {
log.info("redis--------key-----------全部field:" + key);
ShardedJedis jedis = null;
Set<String> value = new HashSet<String>();
try {
jedis = shardedJedisPool.getResource();
value = jedis.hkeys(key);
} catch (RuntimeException e) {
if (jedis != null) {
shardedJedisPool.returnBrokenResource(jedis);
}
e.printStackTrace();
} finally {
// 正確釋放資源
if (jedis != null) {
shardedJedisPool.returnResource(jedis);
}
}
log.info("redis--------key-----------全部field:" + key);
return value;
}
/**
*
* @Title: hvals
* @Description: 得到哈希表中key對應的全部values。
* @param @param key
* @param @return 設定文件
* @return List<String> 返回類型
* @throws
*/
public List<String> hvals(String key) {
log.info("redis--------獲取key中全部的value:" + key);
ShardedJedis jedis = null;
List<String> value = new ArrayList<String>();
try {
jedis = shardedJedisPool.getResource();
value = jedis.hvals(key);
} catch (RuntimeException e) {
if (jedis != null) {
shardedJedisPool.returnBrokenResource(jedis);
}
e.printStackTrace();
} finally {
// 正確釋放資源
if (jedis != null) {
shardedJedisPool.returnResource(jedis);
}
}
log.info("redis--------獲取key中全部的value:" + key);
return value;
}
/**
*
* @Title: hgetAll
* @Description: 返回哈希表key中,全部的域和值。
* @param @param key
* @param @return 設定文件
* @return Map<String,String> 返回類型
* @throws
*/
public Map<String, String> hgetAll(String key) {
log.info("redis--------獲取key中全部的value:" + key);
ShardedJedis jedis = null;
Map<String, String> value = new HashMap<String,String>();
try {
jedis = shardedJedisPool.getResource();
value = jedis.hgetAll(key);
} catch (RuntimeException e) {
if (jedis != null) {
shardedJedisPool.returnBrokenResource(jedis);
}
e.printStackTrace();
} finally {
// 正確釋放資源
if (jedis != null) {
shardedJedisPool.returnResource(jedis);
}
}
log.info("redis--------獲取key中全部的value:" + key);return value;}