redisTemplate的spring配置以及lua脚本驱动

发布时间:2017-3-25 10:01:32 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"redisTemplate的spring配置以及lua脚本驱动 ",主要涉及到redisTemplate的spring配置以及lua脚本驱动 方面的内容,对于redisTemplate的spring配置以及lua脚本驱动 感兴趣的同学可以参考一下。

  最近在使用spring-data-redis的redisTemplate,所以写篇使用记录吧。

  1.不用多说,使用maven引入相关依赖,因为项目已经引入其他的

  

<dependency>            <groupId>org.springframework.data</groupId>            <artifactId>spring-data-redis</artifactId>            <version>1.6.2.RELEASE</version>            <exclusions>                <exclusion>                    <artifactId>spring-context</artifactId>                    <groupId>org.springframework</groupId>                </exclusion>                <exclusion>                    <artifactId>spring-aop</artifactId>                    <groupId>org.springframework</groupId>                </exclusion>                <exclusion>                    <artifactId>spring-context-support</artifactId>                    <groupId>org.springframework</groupId>                </exclusion>                <exclusion>                    <artifactId>spring-tx</artifactId>                    <groupId>org.springframework</groupId>                </exclusion>                <exclusion>                    <artifactId>spring-core</artifactId>                    <groupId>org.springframework</groupId>                </exclusion>                <exclusion>                    <artifactId>slf4j-api</artifactId>                    <groupId>org.slf4j</groupId>                </exclusion>            </exclusions>        </dependency>

 2.spring配置文件

<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>    <!-- Jedis ConnectionFactory -->    <bean id="jedisConnectionFactory"        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"        p:hostName="${redis.url}" p:port="${redis.port}" p:password="${redis.password}" p:pool-config-ref="poolConfig"/>    <bean id="stringRedisSerializer"        class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>    <bean id="jsonSerializer"        class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer">    </bean>    <!-- redis template definition -->    <bean id="redisTemplate" name="redisTemplate"        class="org.springframework.data.redis.core.RedisTemplate"        p:connection-factory-ref="jedisConnectionFactory" p:keySerializer-ref="stringRedisSerializer"        p:hashKeySerializer-ref="stringRedisSerializer" p:valueSerializer-ref="jsonSerializer"        p:hashValueSerializer-ref="jsonSerializer">    </bean>

  假如要使用redis驱动lua脚本则需要加入类似的配置

<bean id="updateAvailableSavingsCard" class="org.springframework.data.redis.core.script.DefaultRedisScript">        <property name="location" value="classpath:META-INF/lua/updateAvailableSavingsCard.lua"/>        <property name="resultType" value="org.meibaobao.ecos.basecomponent.common.Result"/>    </bean>    <bean id="initAvailableSavingsCard" class="org.springframework.data.redis.core.script.DefaultRedisScript">        <property name="location" value="classpath:META-INF/lua/initAvailableSavingsCard.lua"/>        <property name="resultType" value="java.lang.Boolean"/>    </bean>

  lua脚本文件(updateAvailableSavingsCard.lua)

-- 操作可用储蓄卡,当客户购买则减,卖家增加可用库存则加,使用lua脚本redis操作的保证原子性redis.call('INCRBY', KEYS[1], ARGV[1])local current = redis.call('GET', KEYS[1])local result ={}result["@class"] = "org.meibaobao.ecos.basecomponent.common.Result"if tonumber(current)<0then    redis.call('DECRBY', KEYS[1], ARGV[1])    current = redis.call('GET', KEYS[1])    result["success"] = false    result["data"] = current    local encodestr = cjson.encode(result)    print(encodestr)    return  encodestrendresult["success"] = trueresult["data"] = currentlocal encodestr = cjson.encode(result)print(encodestr)return  encodestr

3.资源文件

#redisredis.url=10.72.82.124redis.port=6379redis.password=redis.maxIdle=200redis.maxActive=1024redis.maxWait=1000redis.testOnBorrow=true

4.java代码

package org.meibaobao.ecos.storedCard.scheme.service.component.impl;import java.util.Collections;import javax.annotation.Resource;import org.meibaobao.ecos.basecomponent.common.Result;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.script.RedisScript;import org.springframework.stereotype.Service;/** * 储蓄卡资源 * */@Servicepublic class StoredCardInventoryServiceImpl {    @Resource(name = "updateAvailableSavingsCard")    RedisScript<Result> updateAvailableSavingsCard;    @Resource(name = "initAvailableSavingsCard")    RedisScript<Boolean> initAvailableSavingsCard;        @Autowired    RedisTemplate<String,Integer> redisTemplate;        public static String availableStotredCardInventoryNamePre = "availableStotredCard_";    /**     * 操作可用资源     */    public Result availableStotredCardInventory(String storedCardSchemeNo,int num){        return redisTemplate.execute(updateAvailableSavingsCard,  Collections.singletonList(availableStotredCardInventoryNamePre+storedCardSchemeNo),num);    }    /**     * 初始化可用资源     */    public Boolean initAvailableStotredCardInventory(String storedCardSchemeNo,int num){        return redisTemplate.execute(initAvailableSavingsCard,  Collections.singletonList(availableStotredCardInventoryNamePre+storedCardSchemeNo),                num);    }}

5.junit4单元测试代码

package org.meibaobao.ecos.storedCard.scheme.service.component.impl;import java.util.Map;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = { "classpath:development/xml/spring/applicationContext.xml" })public class StotredCardInventoryServiceImplTest {        @Resource    private StoredCardInventoryServiceImpl stotredCardInventoryServiceImpl;        @Autowired    RedisTemplate<String,Object> redisTemplate;        @Test    public void availableStotredCardInventory() {        System.out.println(stotredCardInventoryServiceImpl.initAvailableStotredCardInventory("test1111112",-10));        System.out.println(stotredCardInventoryServiceImpl.initAvailableStotredCardInventory("test1111112",-10));                redisTemplate.opsForValue().set("test99999", "123asd");                System.out.println(redisTemplate.opsForValue().get("test99999"));                            //测试hash        String key = "hashtest11";        redisTemplate.opsForHash().put(key, "name1", "11");        redisTemplate.opsForHash().put(key, "name2", "22");        redisTemplate.opsForHash().put(key, "name3", "33");                System.out.println(redisTemplate.opsForHash().get(key, "name2"));        redisTemplate.opsForHash().delete(key, "name2");        System.out.println(redisTemplate.opsForHash().get(key, "name2"));        System.out.println(redisTemplate.opsForHash().get(key, "name1"));                Map<Object, Object> opsMap = redisTemplate.opsForHash().entries(key);        if(opsMap != null) {            System.out.println(opsMap.keySet());            System.out.println(opsMap.values());            for(Object object : opsMap.keySet()) {                System.out.println(object + "," + opsMap.get(object));            }

上一篇:Asp.Net MVC 在后台获取PartialView、View文件生成的字符串
下一篇:[linux]解决vsftpd 读取目录列表失败的问题

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

好贷网好贷款