reids简单应用

首先安装redis 不细描述了,我的是安装在ubuntu16.0.4上

然后启动redis  cd到redis的安装目录下   然后redis.server redis.conf  启动,

之前我在只执行redis.server 的时候好像不行,

记得在redis.conf  配置开启远程访问

 启动起来了



 测试成功

 

下面开始我们的redis 跟spring的结合

maven 配置引入

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.7.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.3</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.13</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.10</version>
        </dependency>

 jar包

 

spring-redis 文件配置

<?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:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       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:component-scan base-package="com.guo.*"
                            annotation-config="true" />

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="10"></property>
        <property name="maxIdle" value="10"></property>
        <property name="minIdle" value="2"></property>
        <property name="maxWaitMillis" value="15000"></property>
        <property name="minEvictableIdleTimeMillis" value="300000"></property>
        <property name="numTestsPerEvictionRun" value="3"></property>
        <property name="timeBetweenEvictionRunsMillis" value="60000"></property>
        <property name="testOnBorrow" value="true"></property>
        <property name="testOnReturn" value="true"></property>
        <property name="testWhileIdle" value="true"></property>
    </bean>

    <bean id="jedisConnectionFactory"
          class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          destroy-method="destroy">
        <property name="hostName" value="192.168.1.112" />
        <property name="port" value="6379" />
        <property name="timeout" value="15000" />
        <property name="database" value="0" />
        <property name="usePool" value="true" />
        <property name="poolConfig" ref="jedisPoolConfig" />
    </bean>

    <!-- redis template definition p表示对该bean里面的属性进行注入,格式为p:属性名=注入的对象 效果与在bean里面使用<property>标签一样 -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"
          p:connection-factory-ref="jedisConnectionFactory">
        <!-- 序列化方式 建议key/hashKey采用StringRedisSerializer。 -->
        <property name="keySerializer">
            <bean
                    class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="hashKeySerializer">
            <bean
                    class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean
                    class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>
        <property name="hashValueSerializer">
            <bean
                    class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>

    </bean>
    <!-- 对string操作的封装 -->
    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"
          p:connection-factory-ref="jedisConnectionFactory" />
    <!--         <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager" c:template-ref="redisTemplate"/> -->

</beans>

 

然后就是service中的应用

  @Resource(name="redisTemplate")
     RedisTemplate redisTemplate;

 

@Override
    public User getUser(final String id) {
        ValueOperations<String, User> valueops = redisTemplate
                     .opsForValue();
              User user = valueops.get(id);
              return user;
    }


 @Override
    public void saveUser( User user) {
      // redisTemplate.opsForValue().set("guo", "ok");
//        redisTemplate.execute(new RedisCallback<Object>() {
//
//            @Override
//            public Object doInRedis(RedisConnection connection) throws DataAccessException {
//                connection.set(redisTemplate.getStringSerializer().serialize("user.uid." + user.getUserid()),
//                        redisTemplate.getStringSerializer().serialize(user.getProfilephoto()));
//                return null;
//            }
//        });
//        ValueOperations<String, User> valueops = redisTemplate.opsForValue();
        ValueOperations<String, User> valueops = redisTemplate.opsForValue();
        valueops.set(user.getUserid(), user);

    }

 

对list的存储

 

    @Override
    public void saveUserList() {
        // redisTemplate.opsForValue().set("guo", "ok");
//        redisTemplate.execute(new RedisCallback<Object>() {
//
//            @Override
//            public Object doInRedis(RedisConnection connection) throws DataAccessException {
//                connection.set(redisTemplate.getStringSerializer().serialize("user.uid." + user.getUserid()),
//                        redisTemplate.getStringSerializer().serialize(user.getProfilephoto()));
//                return null;
//            }
//        });
//        ValueOperations<String, User> valueops = redisTemplate.opsForValue();
        List<User> list=new ArrayList<User>();
        for(int i=0;i<=5;i++){
            User user=new User();
            user.setUserid(""+i);
            list.add(user);
        }
        ListOperations<String, List> valueops = redisTemplate.opsForList();
        valueops.leftPush("list",list);

    }




  @Override
    public List<User> getUserList() {
        // redisTemplate.opsForValue().set("guo", "ok");
//        redisTemplate.execute(new RedisCallback<Object>() {
//
//            @Override
//            public Object doInRedis(RedisConnection connection) throws DataAccessException {
//                connection.set(redisTemplate.getStringSerializer().serialize("user.uid." + user.getUserid()),
//                        redisTemplate.getStringSerializer().serialize(user.getProfilephoto()));
//                return null;
//            }
//        });
//        ValueOperations<String, User> valueops = redisTemplate.opsForValue();


        ListOperations<String, List> valueops = redisTemplate.opsForList();
         List<User> list= valueops.leftPop("list");
         return list;

    }

 redis.opsForValue()  封装操作strings

redis.opsForList()   封装操作list
redis.opsForSet() 封装操作sets
redis.opsForZSet() 封装操作sorted sets
redis.opsForHash() 封装操作hashs
 
其他的在实际运用中再补充
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值