Spring集成Redis配置
文件清单:
reids.properties
applicationContext.xml
RedisTest .java
1)reids.properties
#最大分配的链接数
redis.pool.maxActive=1024
#最大能够保持空闲状态的链接数
redis.pool.maxIdle=200
#当池内没有返回链接时,最大等待时间
redis.pool.maxWait=1000
#当调用borrow Object方法时,是否进行有效性检查
redis.pool.testOnBorrow=true
#当调用return Object方法时,是否进行有效性检查
redis.pool.testOnReturn=true
#IP
redis.ip=127.0.0.1
#Port
redis.port=6379
#我的配置 Redis settings
redis.host=192.168.59.130
redis.port=6379
redis.pass=neusoft,123
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
2)applicationContext.xml
<!--引入资源文件:-->
<context:property-placeholder
location="classpath:config/db.properties,classpath:config/redis.properties" />
<!-- redis配置 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxActive}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="usePool" value="true"></property>
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<!-- <property name="password" value="${redis.pass}" /> -->
<property name="timeout" value="${redis.maxWait}" />
<!-- <property name="database" value="${redis.default.db}"></property> -->
<constructor-arg index="0" ref="jedisPool" />
</bean>
<!-- 在RedisTemplate中针对不同类型的数据提供了不同的序列化方式。 默认的序列化方式为JdkSerializationRedisSerializer。
而我们常用的配置为键采用StringRedisSerializer来序列化,value采用默认的JdkSerializationSerializer。这就是为什么我们使用redis缓存对象时候需要让对象实现java.io.Serializable
序列化接口的原因。 https://www.cnblogs.com/cuglkb/p/6895423.html -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<!-- 键序列化方式 -->
<property name="keySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<!-- 值序列化方式 -->
<property name="valueSerializer">
<!-- <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"
/> -->
<bean
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
</bean>
RedisTest .java
public class RedisTest {
public static void main(String[] args) {
/**
* Redis五大类型:字符串(String)、哈希/散列/字典(Hash)、列表(List)、集合(Set)、有序集合(sorted set)五种
Controller:@Resource RedisTemplate<String, String> redisTemplate;
总括:
redisTemplate.opsForValue();//操作字符串
redisTemplate.opsForHash();//操作hash
redisTemplate.opsForList();//操作list
redisTemplate.opsForSet();//操作set
redisTemplate.opsForZSet();//操作有序set
*/
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
RedisTemplate<String, Object> redisTemplate = ctx.getBean("redisTemplate",RedisTemplate.class);
//添加一个 key
ValueOperations<String, Object> value = redisTemplate.opsForValue();
value.set("lp", "hello word");
//获取 这个 key 的值
System.out.println(value.get("lp"));
//添加 一个 hash集合
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
Map<String,Object> map = new HashMap<String,Object>();
map.put("name","wang.qj");
map.put("age", "30");
hash.putAll("lpMap", map);
//获取 map
// System.out.println(hash.entries("lpMap"));
System.out.println("lpMap-name------"+hash.get("lpMap", "name"));
//添加 一个 list 列表
ListOperations<String, Object> list = redisTemplate.opsForList();
list.rightPush("lpList", "lp");
list.rightPush("lpList", "26");
//输出 list
System.out.println(list.range("lpList", 0, 1));
//添加 一个 set 集合
SetOperations<String, Object> set = redisTemplate.opsForSet();
set.add("lpSet", "lp");
set.add("lpSet", "26");
set.add("lpSet", "178cm");
//输出 set 集合
System.out.println(set.members("lpSet"));
//添加有序的 set 集合
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
zset.add("lpZset", "lp", 0);
zset.add("lpZset", "26", 1);
zset.add("lpZset", "178cm", 2);
//输出有序 set 集合
System.out.println(zset.rangeByScore("lpZset", 0, 2));
}
}
网友评论