redis初级入门,整合spring应用

linux下相关redis命令 java

tar zxf redis-2.7.0.tar.gz
cd redis-2.7.0
make
安装完毕。linux

mkdir /usr/local/redis
cd src
cp redis-server redis-benchmark redis-cli ../redis.conf /usr/local/redisweb

./redis-cli 客户端连接 redis

/etc/init.d/redis status|start|stop|    查看状态,启动,中止spring

 Redis配置文件(redis.conf)可在Redis的根目录下找到。能够经过Redis的CONFIG命令设置全部Redis的配置。服务器

进入客户端后,命令 config get * 获取全部的配置,maven

config set maxclients 1000 设置最大客户端链接数,spa

config get maxclient   获得最大客户端链接数orm

maven  redis依赖配置server

<sring-data-redis.version>1.5.0.RELEASE</sring-data-redis.version>
        <jedis.version>2.6.2</jedis.version>

<dependency>
        <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>${sring-data-redis.version}</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>${jedis.version}</version>
 </dependency>

Spring相关配置

    spring.redis.pool.host=10.13.0.179   redis服务器路径

   <!-- redis -->
     <bean id="jedisConnFactory"
          class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${spring.redis.pool.host}"/>

        <property name="port" value="3333"/>
        <property name="usePool" value="true"/>
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnFactory"/>
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
    </bean>

java实例代码

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

往redis里添加一条键值对形式的数据,设置有效时间为1分钟。

redisTemplate.opsForValue().set(key, value, 1,TimeUnit.MINUTES);

redis采用随机删除方式,1分钟之后并不必定会删除该条数据,可是会标记该条记录已无效。1分钟后,经过get(key)方式获取为null,

redisTemplate.opsForValue().get(key);

相关文章
相关标签/搜索