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

詳解java之redis篇(spring-data-redis整合)

 更新時(shí)間:2017年01月09日 16:05:23   作者:tankaixiong  
本篇文章主要介紹了java之redis篇,主要詳細(xì)的介紹了spring-data-redis整合,有興趣的可以了解一下。

1,利用spring-data-redis整合

項(xiàng)目使用的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.x.redis</groupId>
 <artifactId>Spring_redis</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>Spring_redis</name>
 <url>http://maven.apache.org</url>

 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

 <dependencies>
  <dependency> 
  <groupId>org.springframework.data</groupId> 
  <artifactId>spring-data-redis</artifactId> 
  <version>1.0.2.RELEASE</version> 
 </dependency> 
 <dependency> 
  <groupId>org.springframework</groupId> 
  <artifactId>spring-core</artifactId> 
  <version>3.1.2.RELEASE</version> 
 </dependency> 

  
 <dependency> 
  <groupId>redis.clients</groupId> 
  <artifactId>jedis</artifactId> 
  <version>2.1.0</version> 
 </dependency> 
  
  <dependency> 
  <groupId>junit</groupId> 
  <artifactId>junit</artifactId> 
  <version>4.8.2</version> 
  <scope>test</scope> 
 </dependency> 
    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.6.1</version>
   </dependency>
   <!-- 將現(xiàn)有的jakarta commons logging的調(diào)用轉(zhuǎn)換成lsf4j的調(diào)用。 -->
   <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.6.1</version>
   </dependency>
   <!-- Hack:確保commons-logging的jar包不被引入,否則將和jcl-over-slf4j沖突 -->
   <dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.1</version>
    <scope>provided</scope>
   </dependency>
   <!-- slf4j的實(shí)現(xiàn):logback,用來取代log4j。更快、更強(qiáng)! -->
   <dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>0.9.24</version>
    <scope>runtime</scope>
   </dependency>
 </dependencies>
</project>

除了log部分,只有一個(gè)spring core 和 spring-data-redis了

項(xiàng)目文件目錄結(jié)構(gòu):

applicationContext.xml:

1,context:property-placeholder 標(biāo)簽用來導(dǎo)入properties文件。從而替換${redis.maxIdle}這樣的變量。

2,context:component-scan 是為了在com.x.redis.dao報(bào)下的類能夠?qū)嵱胹pring的注解注入的方式。

3,事實(shí)上我們只需要把JedisPoolConfig配數(shù)來就好了,接下來就是spring的封裝了。所以直接看UserDAOImpl的實(shí)現(xiàn)就明白了。

<?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" 
 xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 
 xmlns:aop="http://www.springframework.org/schema/aop" 
 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.xsd"> 
 
 <context:property-placeholder location="classpath:redis.properties" /> 
 <context:component-scan base-package="com.x.redis.dao">
 </context:component-scan>
 <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> 
  <property name="maxIdle" value="${redis.maxIdle}" /> 
  <property name="maxActive" value="${redis.maxActive}" /> 
  <property name="maxWait" value="${redis.maxWait}" /> 
  <property name="testOnBorrow" value="${redis.testOnBorrow}" /> 
 </bean> 
  
 <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 
  p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/> 
  
 <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> 
  <property name="connectionFactory" ref="connectionFactory" /> 
 </bean>   
  
 <bean id="userDAO" class="com.x.redis.dao.impl.UserDAOImpl" /> 
</beans>

redis.properties:

# Redis settings
#redis.host=192.168.20.101
#redis.port=6380
#redis.pass=foobared
redis.host=127.0.0.1
redis.port=6379
redis.pass=
 
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true

UserDAOImpl:

1,spring對(duì)dao層的封裝很多用了類似于下面代碼的模板方式。

2,RedisTemplate就是spring對(duì)redis的一個(gè)封裝而已。

public class UserDAOImpl implements UserDAO {

 @Autowired
 protected RedisTemplate<Serializable, Serializable> redisTemplate;

 public void saveUser(final User user) {
  redisTemplate.execute(new RedisCallback<Object>() {

   @Override
   public Object doInRedis(RedisConnection connection) throws DataAccessException {
    connection.set(redisTemplate.getStringSerializer().serialize("user.uid." + user.getId()),
        redisTemplate.getStringSerializer().serialize(user.getName()));
    return null;
   }
  });
 }

 @Override
 public User getUser(final long id) {
  return redisTemplate.execute(new RedisCallback<User>() {
   @Override
   public User doInRedis(RedisConnection connection) throws DataAccessException {
    byte[] key = redisTemplate.getStringSerializer().serialize("user.uid." + id);
    if (connection.exists(key)) {
     byte[] value = connection.get(key);
     String name = redisTemplate.getStringSerializer().deserialize(value);
     User user = new User();
     user.setName(name);
     user.setId(id);
     return user;
    }
    return null;
   }
  });
 }

 
}

其他:

User:

public class User {

 private long id;
 private String name;
 
 public long getId() {
  return id;
 }
 
 public void setId(long id) {
  this.id = id;
 }
 
 public String getName() {
  return name;
 }
 
 public void setName(String name) {
  this.name = name;
 }
}

測試代碼:

public static void main(String[] args) {
  ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");
  UserDAO userDAO = (UserDAO)ac.getBean("userDAO");
  User user1 = new User();
  user1.setId(1);
  user1.setName("obama");
  userDAO.saveUser(user1);
  User user2 = userDAO.getUser(1);
  System.out.println(user2.getName());
 }

2,不利用spring-data-redis整合

個(gè)人覺得這樣整合靈活度更大,能夠更加明了的完成任務(wù)。

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.d.work</groupId>
 <artifactId>Redis_Templete</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>Redis_Templete</name>
 <url>http://maven.apache.org</url>

 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

 <dependencies>
 <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
 </dependency>
 <dependency> 
  <groupId>redis.clients</groupId> 
  <artifactId>jedis</artifactId> 
  <version>2.1.0</version> 
 </dependency> 
  <dependency> 
  <groupId>org.springframework</groupId> 
  <artifactId>spring-core</artifactId> 
  <version>3.1.2.RELEASE</version> 
 </dependency> 
  <dependency> 
  <groupId>org.springframework</groupId> 
  <artifactId>spring-beans</artifactId> 
  <version>3.1.2.RELEASE</version> 
 </dependency> 
  <dependency> 
  <groupId>org.springframework</groupId> 
  <artifactId>spring-context</artifactId> 
  <version>3.1.2.RELEASE</version> 
 </dependency> 
 <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.6.1</version>
   </dependency>
   <!-- 將現(xiàn)有的jakarta commons logging的調(diào)用轉(zhuǎn)換成lsf4j的調(diào)用。 -->
   <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.6.1</version>
   </dependency>
   <!-- Hack:確保commons-logging的jar包不被引入,否則將和jcl-over-slf4j沖突 -->
   <dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.1</version>
    <scope>provided</scope>
   </dependency>
   <!-- slf4j的實(shí)現(xiàn):logback,用來取代log4j。更快、更強(qiáng)! -->
   <dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>0.9.24</version>
    <scope>runtime</scope>
   </dependency>
 </dependencies>
</project>

目錄結(jié)構(gòu):

data-source.xml

1,context:property-placeholder 和 context:component-scan 前面解釋過啦。

2,配置了一個(gè)ShardedJedisPool,在jdeis里 還有個(gè)JedisPool。這兩個(gè)的區(qū)別:

一個(gè)是分片形式,可以連接有主備的redis服務(wù)端,一個(gè)是單個(gè)的。詳細(xì)后續(xù)學(xué)習(xí)

3,因?yàn)椴皇褂胹pring-data-redis的封裝,所以自己要自己封裝一個(gè)

<?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" 
 xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 
 xmlns:aop="http://www.springframework.org/schema/aop" 
 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.xsd"> 

 <context:property-placeholder location="classpath:redis.properties" /> 
 <context:component-scan base-package="com.d.work.main">
 </context:component-scan>
  <context:component-scan base-package="com.d.work.redis">
 </context:component-scan>
 <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
  <property name="maxActive" value="50" />
  <property name="maxIdle" value="8" />
  <property name="maxWait" value="1000" />
  <property name="testOnBorrow" value="true"/>
  <property name="testOnReturn" value="true"/>
  <!-- <property name="testWhileIdle" value="true"/> -->
 </bean>

 <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" scope="singleton">
  <constructor-arg index="0" ref="jedisPoolConfig" />
  <constructor-arg index="1">
   <list>
    <bean class="redis.clients.jedis.JedisShardInfo">
     <constructor-arg name="host" value="${redis.host}" />
     <constructor-arg name="port" value="${redis.port}" />
     <constructor-arg name="timeout" value="${redis.timeout}" />
     <constructor-arg name="weight" value="1" />
    </bean>
   </list>
  </constructor-arg>
 </bean>
</beans>

RedisDataSource:定義三個(gè)方法

public interface RedisDataSource {
 public abstract ShardedJedis getRedisClient();
 public void returnResource(ShardedJedis shardedJedis);
 public void returnResource(ShardedJedis shardedJedis,boolean broken);
}

實(shí)現(xiàn)redisDataSource:

1, 注入配置好的ShardedJedisPool,這三個(gè)方法的作用:

  •  getRedisClient() : 取得redis的客戶端,可以執(zhí)行命令了。
  • returnResource(ShardedJedis shardedJedis) : 將資源返還給pool
  • returnResource(ShardedJedis shardedJedis, boolean broken) : 出現(xiàn)異常后,將資源返還給pool (其實(shí)不需要第二個(gè)方法)
@Repository("redisDataSource")
public class RedisDataSourceImpl implements RedisDataSource {

 private static final Logger log = LoggerFactory.getLogger(RedisDataSourceImpl.class);

 @Autowired
 private ShardedJedisPool shardedJedisPool;

 public ShardedJedis getRedisClient() {
  try {
   ShardedJedis shardJedis = shardedJedisPool.getResource();
   return shardJedis;
  } catch (Exception e) {
   log.error("getRedisClent error", e);
  }
  return null;
 }

 public void returnResource(ShardedJedis shardedJedis) {
  shardedJedisPool.returnResource(shardedJedis);
 }

 public void returnResource(ShardedJedis shardedJedis, boolean broken) {
  if (broken) {
   shardedJedisPool.returnBrokenResource(shardedJedis);
  } else {
   shardedJedisPool.returnResource(shardedJedis);
  }
 }
}

第二層的封裝:RedisClientTemplate,例子實(shí)現(xiàn)了放值和取值。最后代碼提供了全部命令的實(shí)現(xiàn)。

代碼就是映射性質(zhì)的又一次調(diào)用jedis的方法而已,用了個(gè)broken來做標(biāo)示符,決定返還資源的方式。

這一層的目的主要也是讓再上層的調(diào)用不需要關(guān)心pool中鏈接的取得和返還問題了。

@Repository("redisClientTemplate")
public class RedisClientTemplate {

 private static final Logger log = LoggerFactory.getLogger(RedisClientTemplate.class);

 @Autowired
 private RedisDataSource  redisDataSource;

 public void disconnect() {
  ShardedJedis shardedJedis = redisDataSource.getRedisClient();
  shardedJedis.disconnect();
 }

 /**
  * 設(shè)置單個(gè)值
  * 
  * @param key
  * @param value
  * @return
  */
 public String set(String key, String value) {
  String result = null;

  ShardedJedis shardedJedis = redisDataSource.getRedisClient();
  if (shardedJedis == null) {
   return result;
  }
  boolean broken = false;
  try {
   result = shardedJedis.set(key, value);
  } catch (Exception e) {
   log.error(e.getMessage(), e);
   broken = true;
  } finally {
   redisDataSource.returnResource(shardedJedis, broken);
  }
  return result;
 }

 /**
  * 獲取單個(gè)值
  * 
  * @param key
  * @return
  */
 public String get(String key) {
  String result = null;
  ShardedJedis shardedJedis = redisDataSource.getRedisClient();
  if (shardedJedis == null) {
   return result;
  }

  boolean broken = false;
  try {
   result = shardedJedis.get(key);

  } catch (Exception e) {
   log.error(e.getMessage(), e);
   broken = true;
  } finally {
   redisDataSource.returnResource(shardedJedis, broken);
  }
  return result;
 }
}

測試代碼:

 public static void main(String[] args) {
  ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/data-source.xml");
  RedisClientTemplate redisClient = (RedisClientTemplate)ac.getBean("redisClientTemplate");
  redisClient.set("a", "abc");
  System.out.println(redisClient.get("a"));
 }

附上RedisClientTemplate全部實(shí)現(xiàn):

RedisClientTemplate代碼太多,附上下載地址:http://xiazai.jb51.net/201701/yuanma/RedisClientTemplate_jb51.rar

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

相關(guān)文章

  • 將SpringBoot的Jar注冊成Windows服務(wù)的實(shí)現(xiàn)方法

    將SpringBoot的Jar注冊成Windows服務(wù)的實(shí)現(xiàn)方法

    當(dāng)前項(xiàng)目有個(gè)地圖編輯器,后端用的是SpringBoot框架,外網(wǎng)剛好有一臺(tái)空閑的Windows服務(wù)器就直接拿來用了,將Java程序部署成Windows服務(wù)可以用WinSW (Windows Service Wrapper)來實(shí)現(xiàn),文中有詳細(xì)的操作步驟,需要的朋友可以參考下
    2023-11-11
  • springsecurity中http.permitall與web.ignoring的區(qū)別說明

    springsecurity中http.permitall與web.ignoring的區(qū)別說明

    這篇文章主要介紹了springsecurity中http.permitall與web.ignoring的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • JPA多數(shù)據(jù)源分布式事務(wù)處理方案

    JPA多數(shù)據(jù)源分布式事務(wù)處理方案

    這篇文章主要為大家介紹了JPA多數(shù)據(jù)源分布式事務(wù)處理的兩種事務(wù)方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-02-02
  • Java多線程ForkJoinPool實(shí)例詳解

    Java多線程ForkJoinPool實(shí)例詳解

    這篇文章主要介紹了Java多線程ForkJoinPool實(shí)例詳解,涉及forkjoin框架的相關(guān)內(nèi)容,需要的朋友可以參考下。
    2017-09-09
  • 通過Java壓縮JavaScript代碼實(shí)例分享

    通過Java壓縮JavaScript代碼實(shí)例分享

    這篇文章主要介紹了通過Java壓縮JavaScript代碼實(shí)例分享,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-12-12
  • java方法替換word文檔中需要替換的部分操作步驟

    java方法替換word文檔中需要替換的部分操作步驟

    這篇文章主要介紹了java方法替換word文檔中需要替換的部分操作的相關(guān)資料,需要的朋友可以參考下,包括引入依賴、創(chuàng)建業(yè)務(wù)方法、測試以及擴(kuò)展功能,需要的朋友們可以參考下
    2024-12-12
  • Java設(shè)計(jì)模式中的觀察者模式

    Java設(shè)計(jì)模式中的觀察者模式

    觀察者模式定義對(duì)象之間的一種一對(duì)多的依賴關(guān)系,使得每當(dāng)一個(gè)對(duì)象的狀態(tài)發(fā)生變化時(shí),其相關(guān)的依賴對(duì)象都可以得到通知并被自動(dòng)更新。主要用于多個(gè)不同的對(duì)象對(duì)一個(gè)對(duì)象的某個(gè)方法會(huì)做出不同的反應(yīng)
    2023-02-02
  • IDEA集成MyBatis Generator插件的使用

    IDEA集成MyBatis Generator插件的使用

    這篇文章主要介紹了IDEA集成MyBatis Generator插件的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • JAVA實(shí)現(xiàn)的簡單萬年歷代碼

    JAVA實(shí)現(xiàn)的簡單萬年歷代碼

    這篇文章主要介紹了JAVA實(shí)現(xiàn)的簡單萬年歷代碼,涉及Java日期操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • 基于java實(shí)現(xiàn)人機(jī)猜拳游戲

    基于java實(shí)現(xiàn)人機(jī)猜拳游戲

    這篇文章主要為大家詳細(xì)介紹了基于java實(shí)現(xiàn)人機(jī)猜拳游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-12-12

最新評(píng)論

个旧市| 武宁县| 德州市| 奉节县| 正定县| 孙吴县| 平远县| 息烽县| 大竹县| 忻城县| 平武县| 辉县市| 辽阳市| 碌曲县| 平罗县| 曲松县| 灌阳县| 上高县| 丹东市| 夏河县| 泸西县| 江西省| 沽源县| 雅江县| 万载县| 嘉义市| 康定县| 普陀区| 宣汉县| 临澧县| 柳河县| 田阳县| 民勤县| 安仁县| 喀什市| 富裕县| 陇西县| 辽阳市| 嘉鱼县| 大埔县| 河池市|