Redis hash是一个string类型的field和value的映射表.一个key可对应多个field,一个field对应一个value
1、如何主数据在redis缓存中实现初始化?
1)加载主数据,建立MasterMain.java
package com.taikang.healthcare.master.service;
import .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;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;
public class MasterMain implements ApplicationListener<ContextRefreshedEvent> {
private BaseCodeMapper baseCodeMapper; RedisDao redisDao;@Overridepublic void onApplicationEvent(ContextRefreshedEvent event) { if(event.getApplicationContext().getParent() == null){//root application context 没有parent,他就是老大.//需要执行的逻辑代码,当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) ; } }} }
2)启动初始化
在master项目中的applicationContext.xml中增加,启动时调用初始化类加载主数据
<!-- 当Spring容器启动完成后执行下面的这个主数据缓存初始化 -->
<bean class="com.taikang.healthcare.master.service.MasterMain"/>
2、如何实现主数据在redis中的增删改查?
hash数据操作:
1) 使用步骤:
(1)把文件RedisDao因为项目的Dao层中
(2)引入
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;}
目前装载可调用的方法为:
/**
* * : 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) { ("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) { ("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);}}("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) { ("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) { ("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);}}("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) { ("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);}}("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) { ("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);}}("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) { ("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);}}("redis--------获取key中所有的value:" + key);return value;}