Last active
August 29, 2015 14:24
-
-
Save gnosis23/9025eeec7a37799f09f3 to your computer and use it in GitHub Desktop.
Redis配置
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RedisExample { | |
private StringRedisTemplate redisTemplate; | |
public void setRedisTemplate(StringRedisTemplate redisTemplate) { | |
this.redisTemplate = redisTemplate; | |
} | |
private HashOperations<String, String, Long> hashOps; | |
public void setHashOps(HashOperations<String, String, Long> hashOps) { | |
this.hashOps = hashOps; | |
} | |
/** | |
* hincrby key hashkey value | |
* redis能对不存在的值,直接增量 | |
* redis不支持实例化对象 | |
* 但能用hash类型模仿,hash key能带一个属性 | |
* @param merRet 也就是Java中的对象的id值 | |
* @param prop 对象的属性名 | |
*/ | |
protected void addValue(MerRet merRet, String prop, long val) { | |
//hashOps.increment(key, prop, 1); | |
String key = generateId(merRet); | |
redisTemplate.opsForHash().increment(key, prop, val); | |
System.out.printf("%s %s: %s\n", key, prop, hashOps.get(key, prop)); | |
} | |
/** | |
* 根据Id值查找 | |
*/ | |
public void listKeys() { | |
Set<String> keys = redisTemplate.keys("MER*"); | |
for (String key : keys) { | |
System.out.println("KEY: " + key); | |
MerRet mer = extract(key); | |
mer.setPosAmt(getWrapper(key, "amt")); | |
mer.setPtyAmt(getWrapper(key, "cnt")); | |
System.out.println(mer); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 也许要避免包冲突 --> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.data</groupId> | |
<artifactId>spring-data-redis</artifactId> | |
<version>1.5.1.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.commons</groupId> | |
<artifactId>commons-pool2</artifactId> | |
<version>2.0</version> | |
</dependency> | |
<dependency> | |
<groupId>redis.clients</groupId> | |
<artifactId>jedis</artifactId> | |
<version>2.6.2</version> | |
</dependency> | |
</dependencies> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<bean id="jedisConnFactory" | |
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" | |
p:hostName="192.168.123.123" | |
p:port="6379"> | |
</bean> | |
<!-- redis template definition --> | |
<bean id="redisTemplate" | |
class="org.springframework.data.redis.core.RedisTemplate" | |
p:connectionFactory-ref="jedisConnFactory"/> | |
<!--Enable @Autowired, @Resource--> | |
<context:component-scan base-package="com.doremi" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment