zoukankan      html  css  js  c++  java
  • JedisService的编写

         <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-redis</artifactId>
                <version>1.4.3.RELEASE</version>
         </dependency>
         <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.6.0</version>
         </dependency>

    /shangji-manage-web/src/main/resources/jedis.properties

    #最大分配的对象数  ,例如:maxActive=1024//后继版本使用maxTotal属性  
    jedis.maxTotal=5
    #最大能够保持空闲状态的对象数:例如:maxIdle=200 
    jedis.maxIdle=3
    #当池内没有返回对象时,最大等待时间 ,例如:maxWaitMillis=1000
    jedis.maxWaitMillis=10000
    #在borrow(借用/作用)一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的; 
    jedis.testOnBorrow=true
    #redis服务器地址
    redis.host=127.0.0.1
    redis.port=6379

    /shangji-manage-web/src/main/resources/spring/applicationContext.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    
        <!-- 使用spring自带的占位符替换功能 -->
        <bean
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <!-- 允许JVM参数覆盖 -->
            <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
            <!-- 忽略没有找到的资源文件 -->
            <property name="ignoreResourceNotFound" value="true" />
            <!-- 配置资源文件 -->
            <property name="locations">
                <list>
                    <value>classpath:jdbc.properties</value>
                    <value>classpath:env.properties</value>
                    <value>classpath:httpClient.properties</value>
                    <value>classpath:jedis.properties</value>
                    <value>classpath:rabbitmq.properties</value>
                </list>
            </property>
        </bean>
        
        <context:component-scan base-package="com.shangji"/>
        
        <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
            destroy-method="close">
            <!-- 数据库驱动 -->
            <property name="driverClass" value="${jdbc.driver}" />
            <!-- 相应驱动的jdbcUrl -->
            <property name="jdbcUrl" value="${jdbc.url}" />
            <!-- 数据库的用户名 -->
            <property name="username" value="${jdbc.username}" />
            <!-- 数据库的密码 -->
            <property name="password" value="${jdbc.password}" />
            <!-- 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 -->
            <property name="idleConnectionTestPeriod" value="60" />
            <!-- 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 -->
            <property name="idleMaxAge" value="30" />
            <!-- 每个分区最大的连接数 -->
            <property name="maxConnectionsPerPartition" value="150" />
            <!-- 每个分区最小的连接数 -->
            <property name="minConnectionsPerPartition" value="5" />
        </bean>
    
    </beans>

    /shangji-manage-web/src/main/resources/spring/applicationContext-jedis.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
        
        <!-- 池信息 -->
        <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
            <!--#最大分配的对象数 
                    maxActive=1024//后继版本使用maxTotal属性 
                #最大能够保持空闲状态的对象数
                     maxIdle=200 
                #当池内没有返回对象时,最大等待时间 
                    maxWaitMillis=1000 
                #在borrow(借用/作用)一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的; 
                    testOnBorrow=true 
                -->
            <property name="maxTotal" value="${jedis.maxTotal}" />
            <property name="maxIdle" value="${jedis.maxIdle}" />
            <property name="maxWaitMillis" value="${jedis.maxWaitMillis}" />
            <property name="testOnBorrow" value="${jedis.testOnBorrow}" />
        </bean>
    
        <!-- 将   jedisPoolConfig信息   赋给       jedisConnectionFactory的属性 -->
        <bean id="jedisConnectionFactory"
            class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
            <property name="hostName" value="${redis.host}" />
            <property name="port" value="${redis.port}" />
            <property name="poolConfig" ref="jedisPoolConfig" />
        </bean>
         
       <!--  通过     jedisConnectionFactory   生成    连接对象 -->
        <bean class="org.springframework.data.redis.core.StringRedisTemplate"
            p:connection-factory-ref="jedisConnectionFactory" />
        <!-- <context:component-scan base-package="com.shangji" /> -->
    </beans>

    /shangji-common/src/main/java/com/shangji/common/service/JedisService.java

    package com.shangji.common.service;
    
    import java.util.concurrent.TimeUnit;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.data.redis.core.ValueOperations;
    import org.springframework.stereotype.Service;
    
    @Service
    public class JedisService {
        
        @Autowired
        private StringRedisTemplate stringRedisTemplate;//继承了RedisTemplate
        
        // 设置key及value
        public void set(String key,String value){
            // 通过ValueOperations对象操作
            ValueOperations<String, String> vo = stringRedisTemplate.opsForValue();
            vo.set(key, value);
        }
        
        // 设置key 及value和有效时间(单位为小时)
        public void setByHours(String key,String value, Long timeOut){
            // 通过ValueOperations对象操作
            ValueOperations<String, String> vo = stringRedisTemplate.opsForValue();
            vo.set(key, value, timeOut, TimeUnit.HOURS);
        }
        
        // 设置key 及value和有效时间(单位为秒)
        public void setBySeconds(String key,String value, Long timeOut){
            // 通过ValueOperations对象操作
            ValueOperations<String, String> vo = stringRedisTemplate.opsForValue();
            vo.set(key, value, timeOut, TimeUnit.SECONDS);
        }
        
        // 刷新/延长redis中的数据的生存时间(设定的时间单位 为秒)
        public void expire(String key, Long timeout) {
            stringRedisTemplate.expire(key, timeout, TimeUnit.SECONDS);    
        }
        
        // 根据编号查询用户
        public String get(final String key) throws Exception {
            ValueOperations<String, String> vo = stringRedisTemplate.opsForValue();
            return (String) vo.get(key);
        }
        
        public void delete(final String key) {
            // 通过redisTemplate对象直接操作
            stringRedisTemplate.delete(key);
        }
        
    }
    风神
  • 相关阅读:
    css面试题目
    5. React-Router05 BrowserRouter 和hashrouter
    5. React-Router03 withRouter
    5. React-Router01
    CVE-2019-0708—微软RDP远程桌面代码执行漏洞复现
    正则表达式学习汇总
    CTF---PHP安全考题
    快速搭建主动诱导型开源蜜罐框架系统--hfish
    python--爬取网页存在某字符
    Linux 实用指令之查看端口开启情况
  • 原文地址:https://www.cnblogs.com/wu-1393180819/p/9444351.html
Copyright © 2011-2022 走看看