麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 數據庫 > Redis > 正文

詳解SSH框架和Redis的整合

2020-03-17 12:38:42
字體:
來源:轉載
供稿:網友

一個已有的Struts+Spring+Hibernate項目,以前使用MySQL數據庫,現在想把Redis也整合進去。

1. 相關Jar文件

下載并導入以下3個Jar文件:

commons-pool2-2.4.2.jar、jedis-2.3.1.jar、spring-data-redis-1.3.4.RELEASE.jar。

2. Redis配置文件

在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>

3. Redis類

新建一個com.school.redisclient包,結構如下:

ssh整合redis教程,ssh整合redis,ssh,redis

接口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; }}

4. 查詢功能的實現

新建一個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文件就不詳細講了。

5. Redis數據庫

Redis數據庫里面的內容(使用的是Redis Desktop Manager):

ssh整合redis教程,ssh整合redis,ssh,redis

最后是運行結果:

ssh整合redis教程,ssh整合redis,ssh,redis

當然,這只是實現了從Redis查詢數據,還沒有實現將Redis作為MySQL的緩存。

5. 添加功能的實現

新建一個Action:RClasAction,實現向Redis添加課程數據,并同步到MySQL。

package com.school.action;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import com.opensymphony.xwork2.ActionSupport;import com.school.entity.Clas;import com.school.redisclient.RedisService;import com.school.redisclient.RedisTool;import com.school.service.ClasService;@SuppressWarnings("serial")public class RClasAction extends ActionSupport {  @Autowired private ClasService clasService;  RedisService rs = RedisTool.getRedisService(); List<Clas> claslist = new ArrayList<Clas>();  private Clas clas; public Clas getClas() {  return clas; }  public void setClas(Clas Clas) {  this.clas = Clas; }  public String execute(){  saveClas(clas);  return SUCCESS; }  @SuppressWarnings({ "rawtypes", "unchecked" }) private void saveClas(Clas c){  List<String> ids = rs.getList("clas:id");  // clas:id  int num = ids.size();  int id = Integer.parseInt(ids.get(num-1)) + 1;  rs.rightPushList("clas:id", String.valueOf(id));  // clas:count  int count = Integer.parseInt(rs.get("clas:count"));  rs.set("clas:count", String.valueOf(count+1), -1);  // 增加  HashMap hashmap = new HashMap();  hashmap.put("ID", String.valueOf(id));  hashmap.put("NAME", clas.getName());  hashmap.put("COMMENT", clas.getComment());  rs.addHash("clas:" + id, hashmap);  // 同步到MySQL  clasService.saveClas(clas); }}

clas:id是一個List類型的Key-Value,記錄了所有的課程ID,取出最后一個ID,再+1,作為增加的課程的ID,同時clas:count的值也要+1。使用addHash()方法向Redis添加了一個Hash類型的Key-Value(也就是一門課程):

  @SuppressWarnings({ "unchecked", "rawtypes" })  public synchronized void addHash(K key, HashMap map){   redisTemplate.opsForHash().putAll(key, map);  }

同時將該門課程增加到MySQL。

6. 刪除功能的實現

新建一個Action:RClasDeleteAction,實現刪除Redis的課程數據,并同步到MySQL。

package com.school.action;import org.springframework.beans.factory.annotation.Autowired;import com.opensymphony.xwork2.ActionSupport;import com.school.redisclient.RedisService;import com.school.redisclient.RedisTool;import com.school.service.ClasService;@SuppressWarnings("serial")public class RClasDeleteAction extends ActionSupport {  @Autowired private ClasService clasService;  RedisService rs = RedisTool.getRedisService()  private int id; public int getId(){  return id; } public void setId(int id){  this.id=id; }  public String execute(){   deleteClas(id);  // 同步到MySQL  clasService.deleteClas(id);  return SUCCESS; } private void deleteClas(int id){  // 刪除  rs.del("clas:" + id);  // clas:count  int count = Integer.parseInt(rs.get("clas:count"));  rs.set("clas:count", String.valueOf(count-1), -1);  // clas:id  rs.delListItem("clas:id", String.valueOf(id)); }}

直接刪除clas:id,再將clas:count的值-1,這兩步比較簡單,復雜的是從clas:id中刪除該課程的ID,使用了delListItem()方法來實現:

  @Override  public synchronized void delListItem(K key, V value){   redisTemplate.opsForList().remove(key, 1, value);  }

redisTemplate.opsForList().remove()方法類似于LREM命令。最后在MySQL中也刪除相同的課程。

7. 修改功能的實現

新建一個Action:RClasUpdateAction,實現刪除Redis的課程數據,并同步到MySQL。

package com.school.action;import java.util.HashMap;import org.springframework.beans.factory.annotation.Autowired;import com.opensymphony.xwork2.ActionSupport;import com.school.entity.Clas;import com.school.redisclient.RedisService;import com.school.redisclient.RedisTool;import com.school.service.ClasService;@SuppressWarnings("serial")public class RClasUpdateAction extends ActionSupport{  @Autowired private ClasService clasService;  RedisService rs = RedisTool.getRedisService();  private Clas clas; public Clas getClas() {  return clas; } public void setClas(Clas clas) {  this.clas = clas; }  @SuppressWarnings({ "unchecked", "rawtypes" }) public String execute(){  HashMap hashmap = new HashMap();  hashmap.put("ID", String.valueOf(clas.getId()));  hashmap.put("NAME", clas.getName());  hashmap.put("COMMENT", clas.getComment());  rs.putHash("clas:" + clas.getId(), hashmap);  // 同步到MySQL  clasService.updateClas(clas);  return SUCCESS; }}

使用了putHash()方法來更新:

  @SuppressWarnings({ "rawtypes", "unchecked" })  @Override  public synchronized void putHash(K key, HashMap map){   redisTemplate.boundHashOps(key).putAll(map);  }

 同時在MySQL做相同的更新操作。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Redis頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中文字幕极速在线观看 | 免费在线观看一级片 | caoporn国产一区二区 | 亚洲一二区精品 | 中文字幕一区在线观看视频 | 欧美成人精品 | 国产papa | 国产午夜网 | 欧美成人高清视频 | av视在线 | 91成人在线免费 | 天天色综合2 | 国产91在线播放九色 | 噜噜在线视频 | www.成人在线 | 色戒在线版 | 日本黄色免费观看视频 | 中文有码一区二区 | 91福利影视 | 精品在线观看一区二区三区 | 精品成人在线观看 | 黄色片网站在线看 | 国产成年人视频 | 无码av女优 | 欧美日韩视频在线播放 | 毛片午夜| 天天干导航| 国产成人高清成人av片在线看 | 国产精品久久久久久久久久 | 免费一级欧美 | 最近免费观看高清韩国日本大全 | 久久影院在线观看 | 极品国产91在线网站 | 免费观看高清视频网站 | 中文日韩在线视频 | 神马福利电影 | 亚洲3p激情在线观看 | 国产精品一区在线看 | 免费观看高清视频网站 | 一区二区三区黄色 | 99亚洲视频 |