一個已有的Struts+Spring+Hibernate項目,以前使用MySQL數據庫,現在想把Redis也整合進去。
下載並導入以下3個Jar文件:
commons-pool2-2.4.2.jar、jedis-2.3.1.jar、spring-data-redis-1.3.4.RELEASE.jar。
在src文件夾下面新建一個redis.properties文件,設置連接Redis的一些屬性。
redis.host=127.0.0.1 redis.port=6379 redis.default.db=1 redis.timeout=100000 redis.maxActive=300 redis.maxIdle=100 redis.maxWait=1000 redis.testOnBorrow=true
再新建一個redis.xml文件。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:property-placeholder location="classpath:redis.properties"/> <bean id="propertyConfigurerRedis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="order" value="1" /> <property name="ignoreUnresolvablePlaceholders" value="true" /> <property name="systemPropertiesMode" value="1" /> <property name="searchSystemEnvironment" value="true" /> <property name="locations"> <list> <value>classpath:redis.properties</value> </list> </property> </bean> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <bean id="jedisConnectionFactory" 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="timeout" value="${redis.timeout}" /> <property name="database" value="${redis.default.db}"></property> <constructor-arg index="0" ref="jedisPoolConfig" /> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" p:connectionFactory-ref="jedisConnectionFactory" > </bean> <bean id="redisBase" abstract="true"> <property name="template" ref="redisTemplate"/> </bean> <context:component-scan base-package="com.school.redisclient" /> </beans>
新建一個com.school.redisclient包,結構如下:
接口IRedisService:
public interface IRedisService<K, V> { public void set(K key, V value, long expiredTime); public V get(K key); public Object getHash(K key, String name); public void del(K key); }
抽象類AbstractRedisService,主要是對RedisTemplate進行操作:
public abstract class AbstractRedisService<K, V> implements IRedisService<K, V> { @Autowired private RedisTemplate<K, V> redisTemplate; public RedisTemplate<K, V> getRedisTemplate() { return redisTemplate; } public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) { this.redisTemplate = redisTemplate; } @Override public void set(final K key, final V value, final long expiredTime) { BoundValueOperations<K, V> valueOper = redisTemplate.boundValueOps(key); if (expiredTime <= 0) { valueOper.set(value); } else { valueOper.set(value, expiredTime, TimeUnit.MILLISECONDS); } } @Override public V get(final K key) { BoundValueOperations<K, V> valueOper = redisTemplate.boundValueOps(key); return valueOper.get(); } @Override public Object getHash(K key, String name){ Object res = redisTemplate.boundHashOps(key).get(name); return res; } @Override public void del(K key) { if (redisTemplate.hasKey(key)) { redisTemplate.delete(key); } } }
實現類RedisService:
@Service("redisService") public class RedisService extends AbstractRedisService<String, String> { }
工具類RedisTool:
public class RedisTool { private static ApplicationContext factory; private static RedisService redisService; public static ApplicationContext getFactory(){ if (factory == null){ factory = new ClassPathXmlApplicationContext("classpath:redis.xml"); } return factory; } public static RedisService getRedisService(){ if (redisService == null){ redisService = (RedisService) getFactory().getBean("redisService"); } return redisService; } }
新建一個Action:RClasQueryAction,返回Redis裡面所有的課程數據。
@SuppressWarnings("serial") public class RClasQueryAction extends ActionSupport { RedisService rs = RedisTool.getRedisService(); List<Clas> claslist = new ArrayList<Clas>(); Clas c; public String execute(){ if (rs != null){ System.out.println("RedisService : " + rs); getAllClas(); } ServletActionContext.getRequest().setAttribute("claslist", claslist); return SUCCESS; } private void getAllClas(){ claslist = new ArrayList<Clas>(); int num = Integer.parseInt(rs.get("clas:count")); for (int i=0; i<num; i++){ String cid = "clas:" + (i+1); c = new Clas(); int id = Integer.parseInt(String.valueOf(rs.getHash(cid, "ID"))); c.setId(id); System.out.println("ID:" + id); String name = (String) rs.getHash(cid, "NAME"); c.setName(name); System.out.println("NAME:" + name); String comment = (String) rs.getHash(cid, "COMMENT"); c.setComment(comment); System.out.println("COMMENT:" + comment); claslist.add(c); } } }
Struts的設置和jsp文件就不詳細講了。
Redis數據庫裡面的內容(使用的是Redis Desktop Manager):
最後是運行結果:
當然,這只是實現了從Redis查詢數據,還沒有實現將Redis作為MySQL的緩存。
更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2017-01/139354p2.htm