-
Notifications
You must be signed in to change notification settings - Fork 3
redis封装类使用
jqncc edited this page Apr 16, 2019
·
1 revision
两个实现基于的jedis的JedisClientImpl / JedisClusterClientImpl和 基于spring-data-redis的SpringCacheClientImpl
spring声明:
//连接池声明
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxActive}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="maxWaitMillis" value="${redis.pool.maxWait}" />
<property name="testOnBorrow" value="true" />
</bean>
//使用jedis时声明连接
<bean id="jedisConnection" class="org.jflame.toolkit.cache.JedisConnection">
<constructor-arg name="hostName" value="${redis.host}:${cache.redis.port}"></constructor-arg>
<constructor-arg name="mode" value="single"></constructor-arg>
<constructor-arg name="database" value="${redis.db}"></constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
<constructor-arg name="lazy" value="false" ></constructor-arg>
<!-- <property name="password" value="${redis.password}"></property> -->
</bean>
//使用spring redis时声明仍按spring redis
<bean id="redisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<property name="password" value="${redis.password}" />
<property name="usePool" value="true" />
<property name="database" value="${redis.db}"></property>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
</bean>
//静态工厂创建,使用jedis
<bean id="redisClient" class="org.jflame.toolkit.cache.RedisClientFactory" factory-method="createClient">
<constructor-arg index="0" ref="jedisConnection"></constructor-arg>
<!--<constructor-arg index="0" ref="redisConnectionFactory"></constructor-arg>-->
</bean>
代码创建:
//jedis
JedisConnection jedisConn = new JedisConnection(host, db, poolCfg, false);
//spring redis
JedisConnectionFactory jedisConn = new JedisConnectionFactory(poolCfg);
jedisConn.setDatabase(db);
jedisConn.setHostName(host);
jedisConn.setUsePool(true);
jedisConn.afterPropertiesSet();
client = RedisClientFactory.createClient(jedisConn);
使用示例:
client.set(pet.getName(), pet);
long ttl = client.ttl(pet.getName());
Pet cachePet = client.get(pet.getName());
System.out.println("pet ttl:" + ttl);
//hashset
Map<Long,Pet> testMap = new HashMap<>();
testMap.put(999L, new Pet("999kingcat"));
testMap.put(888L, new Pet("888kingpig"));
client.hputAll(hsetkey, testMap);
- 各工具类使用
- redis封装类使用
- zookeeper封装
- 加解密/编码转换
- excel/csv导入导出
- http/ftp工具
- 参数验证
- json工具类使用
- 分布式锁