最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解SSH框架和Redis的整合

 更新時(shí)間:2017年03月25日 16:48:14   作者:MSTK  
本篇文章主要介紹了SSH框架和Redis的整合,詳細(xì)的介紹了Struts+Spring+Hibernate和Redis整合,有興趣的可以了解一下。

一個(gè)已有的Struts+Spring+Hibernate項(xiàng)目,以前使用MySQL數(shù)據(jù)庫,現(xiàn)在想把Redis也整合進(jìn)去。

1. 相關(guān)Jar文件

下載并導(dǎo)入以下3個(gè)Jar文件:

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

2. Redis配置文件

在src文件夾下面新建一個(gè)redis.properties文件,設(shè)置連接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 

再新建一個(gè)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類

新建一個(gè)com.school.redisclient包,結(jié)構(gòu)如下:

接口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,主要是對(duì)RedisTemplate進(jìn)行操作:

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); 
   } 
  } 
  
 } 

實(shí)現(xiàn)類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. 查詢功能的實(shí)現(xiàn)

新建一個(gè)Action:RClasQueryAction,返回Redis里面所有的課程數(shù)據(jù)。

@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的設(shè)置和jsp文件就不詳細(xì)講了。

5. Redis數(shù)據(jù)庫

Redis數(shù)據(jù)庫里面的內(nèi)容(使用的是Redis Desktop Manager):

最后是運(yùn)行結(jié)果:

當(dāng)然,這只是實(shí)現(xiàn)了從Redis查詢數(shù)據(jù),還沒有實(shí)現(xiàn)將Redis作為MySQL的緩存。

5. 添加功能的實(shí)現(xiàn)

新建一個(gè)Action:RClasAction,實(shí)現(xiàn)向Redis添加課程數(shù)據(jù),并同步到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是一個(gè)List類型的Key-Value,記錄了所有的課程ID,取出最后一個(gè)ID,再+1,作為增加的課程的ID,同時(shí)clas:count的值也要+1。使用addHash()方法向Redis添加了一個(gè)Hash類型的Key-Value(也就是一門課程):

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

同時(shí)將該門課程增加到MySQL。

6. 刪除功能的實(shí)現(xiàn)

新建一個(gè)Action:RClasDeleteAction,實(shí)現(xiàn)刪除Redis的課程數(shù)據(jù),并同步到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,這兩步比較簡(jiǎn)單,復(fù)雜的是從clas:id中刪除該課程的ID,使用了delListItem()方法來實(shí)現(xiàn):

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

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

7. 修改功能的實(shí)現(xiàn)

新建一個(gè)Action:RClasUpdateAction,實(shí)現(xiàn)刪除Redis的課程數(shù)據(jù),并同步到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);
  }

 同時(shí)在MySQL做相同的更新操作。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • redis之?dāng)?shù)據(jù)過期清除策略、緩存淘汰策略詳解

    redis之?dāng)?shù)據(jù)過期清除策略、緩存淘汰策略詳解

    這篇文章主要介紹了redis之?dāng)?shù)據(jù)過期清除策略、緩存淘汰策略的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • 為什么RedisCluster設(shè)計(jì)成16384個(gè)槽

    為什么RedisCluster設(shè)計(jì)成16384個(gè)槽

    本文主要介紹了為什么RedisCluster設(shè)計(jì)成16384個(gè)槽,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Redis在Ubuntu系統(tǒng)上無法啟動(dòng)的問題排查

    Redis在Ubuntu系統(tǒng)上無法啟動(dòng)的問題排查

    這篇文章主要介紹了Redis在Ubuntu系統(tǒng)上無法啟動(dòng)的問題排查,文中通過代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-08-08
  • RedisDesktopManager無法遠(yuǎn)程連接Redis的完美解決方法

    RedisDesktopManager無法遠(yuǎn)程連接Redis的完美解決方法

    下載RedisDesktopManager客戶端,輸入服務(wù)器IP地址,端口(缺省值:6379);點(diǎn)擊Test Connection按鈕測(cè)試連接,連接失敗,怎么回事呢?下面小編給大家?guī)砹薘edisDesktopManager無法遠(yuǎn)程連接Redis的完美解決方法,一起看看吧
    2018-03-03
  • redis?setIfAbsent返回null的問題及解決

    redis?setIfAbsent返回null的問題及解決

    這篇文章主要介紹了redis?setIfAbsent返回null的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 深入理解redis刪除策略和淘汰策略

    深入理解redis刪除策略和淘汰策略

    每隔一段時(shí)間就掃描一定數(shù)據(jù)的設(shè)置了過期時(shí)間的key,并清除其中已過期的keys,本文主要介紹了深入理解redis刪除策略和淘汰策略,感興趣的可以了解一下
    2024-08-08
  • redis配置文件redis.conf中文版(基于2.4)

    redis配置文件redis.conf中文版(基于2.4)

    這篇文章主要介紹了redis配置文件redis.conf中文版(基于2.4),對(duì)英文不好的朋友是非常好的參考,需要的朋友可以參考下
    2014-06-06
  • Redis實(shí)現(xiàn)分布式鎖詳解

    Redis實(shí)現(xiàn)分布式鎖詳解

    這篇文章主要介紹了redis如何實(shí)現(xiàn)分布式鎖,文章中有詳細(xì)的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2023-04-04
  • redis添加key幾種方式

    redis添加key幾種方式

    本文主要介紹了redis添加key幾種方式,主要介紹了3種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01
  • RedisAPI原子性操作及原理解析

    RedisAPI原子性操作及原理解析

    這篇文章主要介紹了RedisAPI原子性操作及原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12

最新評(píng)論

遂川县| 日喀则市| 长宁区| 临夏市| 张家口市| 云南省| 绍兴市| 昌都县| 黎平县| 杭锦旗| 济南市| 上栗县| 淮滨县| 阿拉尔市| 西丰县| 吉水县| 乐平市| 山阳县| 密山市| 闵行区| 台北市| 襄汾县| 陵川县| 卓资县| 天峨县| 修武县| 舞阳县| 桂林市| 望城县| 洞头县| 富锦市| 卫辉市| 盐城市| 普洱| 香格里拉县| 沙雅县| 福泉市| 城步| 龙山县| 庄河市| 伊通|