效果
实现
public class JedisPoolUtil { /** * 获取一个redis实例 * @param jedisConnectionFactory * @return */ public static Jedis getJedis(JedisConnectionFactory jedisConnectionFactory){ JedisPoolConfig config = jedisConnectionFactory.getPoolConfig(); JedisPool jedisPool = new JedisPool(config,jedisConnectionFactory.getHostName(),jedisConnectionFactory.getPort()); Jedis jedis=null; try{ jedis=jedisPool.getResource(); }catch(Exception e){ e.printStackTrace(); }finally{ if(jedis!=null){ jedis.close(); } if(jedisPool!=null){ jedisPool.close(); } } return jedis; } }
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy"> <property name="hostName" value="${redis.conn.hostName}" /> <property name="port" value="${redis.conn.port}" /> <property name="timeout" value="${redis.conn.timeout}" /> <property name="database" value="${redis.conn.database}" /> <property name="password" value="${redis.conn.password}" /> <property name="usePool" value="${redis.conn.usePool}" /> <property name="poolConfig" ref="jedisPoolConfig" /> </bean> zookeeper注入
@Override public String getSiteCodeByName(String SiteNameShort) { List<BaseSiteDis> baseSites = baseSiteDao.getFomfSite(); Jedis jedis = JedisPoolUtil.getJedis(jedisConnectionFactory); for (int i = 0; i < baseSites.size(); i++) { BaseSiteDis site = baseSites.get(i); String siteNameShort = site.getSiteNameShort(); String siteStr = JSONObject.toJSONString(site); Map<String, String> map = new HashedMap<>(); map.put(siteNameShort, siteStr); jedis.hmset("FOMF.BASESITE", map); jedis.expire("FOMF.BASESITE", 30);//设置时效实现 秒 } return null; }