本文主要是介绍《权限系列》----用redis存储HashMap,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言
- 小编为了方便,用的是redis的单机版,redis服务器的搭建过程读者自己去百度吧,这里不多说了。
项目源码
点击这里,到github上下载项目的源码
spring配置文件中,redis的配置
<!-- 配置redis客户端单机版 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool"><constructor-arg name="host" value="192.168.141.128"></constructor-arg><constructor-arg name="port" value="6379"></constructor-arg>
</bean>
<!-- 配置redis客户端实现类 -->
<bean id="jedisClientSingle" class="com.spring.mybatis.componet.impl.JedisClientSingle"/>
jedis接口
public interface JedisClient {public String set(String key, String value);public String get(String key);public Long hset(String key, String item, String value);public String hget(String key, String item);public Long incr(String key);public Long decr(String key);public Long expire(String key, int second);public Long ttl(String key);public Long hdel(String key, String item);public String hmset(String key, Map<String,String> map);public List<String> hmget(String key,String filed);public Map<String, String> hgetAll(String key);
}
jedis实现类
public class JedisClientSingle implements JedisClient {@Autowiredprivate JedisPool jedisPool;@Overridepublic String hmset(String key, Map<String, String> map) {Jedis jedis = jedisPool.getResource();String result=jedis.hmset(key,map);jedis.close();return result;}@Overridepublic List<String> hmget(String key, String filed) {Jedis jedis = jedisPool.getResource();List<String> result=jedis.hmget(filed);jedis.close();return result;}@Overridepublic Map<String, String> hgetAll(String key) {Jedis jedis = jedisPool.getResource();Map<String, String> value;value = jedis.hgetAll(key );jedis.close();return value;}@Overridepublic String set(String key, String value) {Jedis jedis = jedisPool.getResource();String result = jedis.set(key, value);if (jedis!=null){jedisPool.returnResource(jedis);}return result;}@Overridepublic String get(String key) {Jedis jedis = jedisPool.getResource();String result = jedis.get(key);if (jedis!=null){jedisPool.returnResource(jedis);}return result;}@Overridepublic Long hset(String key, String item, String value) {Jedis jedis = jedisPool.getResource();Long result = jedis.hset(key, item, value);if (jedis!=null){jedisPool.returnResource(jedis);}return result;}@Overridepublic String hget(String key, String item) {Jedis jedis = jedisPool.getResource();String result = jedis.hget(key, item);if (jedis!=null){jedisPool.returnResource(jedis);}return result;}@Overridepublic Long incr(String key) {Jedis jedis = jedisPool.getResource();Long result = jedis.incr(key);jedis.close();return result;}@Overridepublic Long decr(String key) {Jedis jedis = jedisPool.getResource();Long result = jedis.decr(key);jedis.close();return result;}@Overridepublic Long expire(String key, int second) {Jedis jedis = jedisPool.getResource();Long result = jedis.expire(key, second);jedis.close();return result;}@Overridepublic Long ttl(String key) {Jedis jedis = jedisPool.getResource();Long result = jedis.ttl(key);jedis.close();return result;}@Overridepublic Long hdel(String key, String item) {Jedis jedis = jedisPool.getResource();Long result = jedis.hdel(key, item);jedis.close();return result;}
}
小结
- 小编用的Redis客户端的版本号是2.7.2,故可以用
jedis.close()
来归还资源,如果用的是低版本的那么必须用jedisPool.returnResource(jedis);
来归还资源,要不程序运行一段时间后就会卡死。
这篇关于《权限系列》----用redis存储HashMap的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!