`

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
 
其他的在实际运用中再补充
  • 大小: 29.7 KB
  • 大小: 9.2 KB
分享到:
评论

相关推荐

    Redis简单应用代码

    在“Redis简单应用代码”中,我们可以探讨如何使用Redis进行基本操作,包括连接建立、数据读写以及常见命令的使用。下面将详细介绍Redis的一些核心概念和常用功能。 1. **Redis连接**: - 在编程中,我们需要先...

    Redis简单应用

    在本项目"Redis简单应用"中,我们将探讨如何利用C#编程语言与Redis进行交互,实现基本的数据操作,包括增加、修改、删除和查询。 首先,我们需要在C#项目中引入一个Redis客户端库,例如StackExchange.Redis。这个库...

    多人聊天室代码(mysql以及redis简单应用)

    《多人聊天室代码实现——基于MySQL与Redis的简单应用》 在信息技术日益发达的今天,实时通讯成为了一项基本需求,多人聊天室便是其中的重要应用场景。本项目将介绍如何在Linux环境中利用MySQL和Redis构建一个基础...

    Redis应用场景--Redis作者谈Redis应用场景

    ### Redis应用场景解析 Redis作为一款开源的键值存储系统,凭借其高性能、低延迟的特点,在众多应用场景中展现出独特的价值。本文将围绕Redis作者@antirez分享的几个典型应用场景进行深入探讨,旨在帮助读者更好地...

    WPF操作Redis简单实例

    **标题解析:** "WPF操作Redis简单实例" 这个标题表明了本文将要讨论的是如何在Windows Presentation Foundation (WPF)应用中与Redis数据库进行交互。Redis是一种开源、高性能的键值对数据存储系统,常用于缓存、...

    基于winform的redis的简单应用

    为了实现这个简单的应用,你可能需要在`Redis.sln`项目中包含这些功能,并且在`.vs`目录下可能包含了项目的一些配置信息。`Redis`目录可能包含了具体的Redis操作逻辑代码和其他相关文件。 总之,通过StackExchange....

    java_redis简单案例

    本资源"java_redis简单案例"提供了一个演示如何在Java应用中集成Redis的实例,帮助开发者了解两者的基本用法。 首先,Redis作为内存数据库,其主要特点包括高速读写性能和丰富的数据结构支持,如字符串、哈希、列表...

    Spring data redis的简单应用

    Spring Data Redis是一个强大的Java库,它为开发人员提供了一种简单的方式来使用Redis,这是一个高性能的键值存储系统。本篇文章将深入探讨Spring Data Redis的基本应用,包括如何集成、配置、以及如何利用它来操作...

    redis简单使用实例

    Redis提供了发布/订阅(pub/sub)和阻塞列表(BLPOP/BRPOP)功能,可以实现简单的消息队列。发布/订阅模式用于一对多的消息传递,而阻塞列表则可以实现先进先出(FIFO)的队列服务,常见于工作流调度或者任务分发...

    C#中使用redis简单实例

    通过以上步骤,你可以在C#中构建一个简单的Redis应用,实现键的增删改查。这只是Redis功能的冰山一角,实际应用中还有更多高级特性,如事务、列表、集合、哈希表等,等待你去探索。在实际项目中,根据需求选择合适的...

    Java调用Redis 简单Demo

    本篇将通过一个简单的Java调用Redis的Demo来探讨如何在Java应用中操作Redis。 首先,我们需要在项目中引入Jedis库,这是一个Java客户端,用于连接和操作Redis。你可以通过Maven或Gradle在你的构建文件中添加依赖。...

    springboot连接redis简单示例

    在本文中,我们将深入探讨如何使用Spring Boot框架与Redis数据库进行连接,并实现一个简单的示例。Redis是一个开源的、基于键值对的数据存储系统,常用于缓存、消息队列和数据持久化等多个场景。而Spring Boot是...

    spring+mybatis+redis简单案例

    这是一个基于Spring、MyBatis和Redis的简单案例,旨在演示如何在Java应用中整合这三个关键技术。Spring是一个开源的Java框架,主要用于处理依赖注入和管理应用组件;MyBatis是一个持久层框架,它简化了数据库操作;...

    redis应用场景简介

    去年我写的培训用教材,redis应用场景简介,简单列举了一些Redis的使用场景。 发现下载积分居然无法调整……

    Redis深度历险:核心原理和应用实践.zip

    - **消息队列**:通过发布/订阅模式或列表数据结构,Redis可以实现简单的消息队列功能,用于解耦应用组件。 - **计数器**:利用原子操作特性,Redis可实现对用户行为、访问统计等的实时计数。 - **排序与排行**:...

    最最最简单的SpringBoot+Redis

    标题 "最最最简单的SpringBoot+Redis" 描述了如何在SpringBoot项目中集成和使用Redis,这是一个流行的Java开发框架和一个高效的键值存储系统。接下来,我们将深入探讨这个主题,详细讲解SpringBoot与Redis的集成,...

    Redis简单代码(包含Spring示例)

    学习这部分内容时,你需要理解Redis的数据结构及其适用场景,掌握Spring Data Redis的基本使用,包括配置、模板方法以及如何在实际项目中应用。同时,熟悉Redis的操作命令也是必要的,因为它们可以帮助你更好地理解...

    redis 入门简单demo

    本入门简单Demo将帮助你快速理解Redis的基本概念和操作。以下将详细讲解Redis的关键知识点及其在Demo中的应用。 1. **Redis简介**: Redis是一个开源的内存数据结构存储系统,它支持多种数据结构,如字符串、哈希...

Global site tag (gtag.js) - Google Analytics