网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

SpringDataRedis的使用方法

这篇文章给大家分享的是有关Spring Data redis的使用方法。小编觉得挺实用的,因此分享给大家学习。如下资料是关于Spring Data Redis的实现步骤。

创新互联建站服务项目包括曹县网站建设、曹县网站制作、曹县网页制作以及曹县网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,曹县网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到曹县省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

1.项目常见问题思考

对于电商系统的广告后台管理和广告前台展示,首页每天有大量的人访问,对数据库造成很大的访问压力,甚至是瘫痪。那如何解决呢?我们通常的做法有两种:一种是数据缓存、一种是网页静态化。

2.Redis

redis是一款开源的Key-Value数据库,运行在内存中,由ANSI C编写。企业开发通常采用Redis来实现缓存。同类的产品还有memcache 、memcached 、MongoDB等。

3.Jedis

Jedis是Redis官方推出的一款面向Java的客户端,提供了很多接口供Java语言调用。可以在Redis官网下载,当然还有一些开源爱好者提供的客户端,如Jredis、SRP等等,推荐使用Jedis

4.Spring Data Redis

Spring-data-redis是spring大家族的一部分,提供了在srping应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis,  JRedis, and RJC)进行了高度封装,RedisTemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。
spring-data-redis针对jedis提供了如下功能:

  • 1.连接池自动管理,提供了一个高度封装的“RedisTemplate”类

  • 2.针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口

  • ValueOperations:简单K-V操作

  • SetOperations:set类型数据操作

  • ZSetOperations:zset类型数据操作

  • HashOperations:针对map类型的数据操作

  • ListOperations:针对list类型的数据操作

5.Spring Data Redis实例

准备工作

(1)构建Maven工程  SpringDataRedisDemo
(2)引入Spring相关依赖、引入JUnit依赖   (内容参加其它工程)
(3)引入Jedis和SpringDataRedis依赖
pom.xml


    4.0.0
    com.ldc.org
    SpringDataRedisDemo
    0.0.1-SNAPSHOT

    
    
        4.12
        4.2.4.RELEASE
    

    
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-beans
            ${spring.version}
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
            org.springframework
            spring-aspects
            ${spring.version}
        
        
            org.springframework
            spring-jms
            ${spring.version}
        
        
            org.springframework
            spring-context-support
            ${spring.version}
        
        
            org.springframework
            spring-test
            ${spring.version}
        
        
            junit
            junit
            4.9
        

        
        
            redis.clients
            jedis
            2.8.1
        
        
            org.springframework.data
            spring-data-redis
            1.7.2.RELEASE
        

    

(4)在src/main/resources下创建properties文件夹,建立redis-config.properties

redis.host=127.0.0.1 
redis.port=6379 
redis.pass= 
redis.database=0 
redis.maxIdle=300 
redis.maxWait=3000 
redis.testOnBorrow=true 

(5)在src/main/resources下创建spring文件夹 ,创建applicationContext-redis.xml

 
  

  
      

    
     
        
     
     
     

     

     
          
     

  
  1. maxIdle :最大空闲数
  2. maxWaitMillis:连接时的最大等待毫秒数
  3. testOnBorrow:在提取一个jedis实例时,是否提前进行验证操作;如果为true,则得到的jedis实例均是可用的;

6.值类型操作

package test;

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.SpringJUnit4Cla***unner;

@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class TestValue {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void setValue() {
        redisTemplate.boundValueOps("name").set("ldc");
    }

    @Test
    public void getValue() {
        String string=(String) redisTemplate.boundValueOps("name").get();
        System.out.println(string);
    }

    @Test
    public void deleteValue() {
        redisTemplate.delete("name");
    }

}

7. Set类型操作

package test;

import java.util.Set;

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.SpringJUnit4Cla***unner;

@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class TestSet {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void setValue() {
        //存进去和顺序无关
        redisTemplate.boundSetOps("nameSet").add("曹操");
        redisTemplate.boundSetOps("nameSet").add("刘备");
        redisTemplate.boundSetOps("nameSet").add("孙权");
    }

    @Test
    public void getValue() {
        Set set=redisTemplate.boundSetOps("nameSet").members();
        System.out.println(set);
    }

    @Test
    public void removeValue() {
        //单独的移除其中一个元素
        redisTemplate.boundSetOps("nameSet").remove("孙权");
    }

    @Test
    public void delete() {
        //移除全部
        redisTemplate.delete("nameSet");
    }

}

8.List类型操作

package test;

import java.util.List;
import java.util.Set;

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.SpringJUnit4Cla***unner;

@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class TestList {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 右压栈
     */
    @Test
    public void setValue1() {
        redisTemplate.boundListOps("nameList1").rightPush("刘备");
        redisTemplate.boundListOps("nameList1").rightPush("关羽");
        redisTemplate.boundListOps("nameList1").rightPush("张飞");
    }

    /**
     * 显示右压栈的值
     */
    @Test
    public void getValue1() {
        List list=redisTemplate.boundListOps("nameList1").range(0, 10);
        System.out.println(list);
    }

    /**
     * 左压栈
     */
    @Test
    public void setValue2() {
        redisTemplate.boundListOps("nameList2").leftPush("刘备");
        redisTemplate.boundListOps("nameList2").leftPush("关羽");
        redisTemplate.boundListOps("nameList2").leftPush("张飞");
    }

    /**
     * 显示左压栈的值
     */
    @Test
    public void getValue2() {
        List list=redisTemplate.boundListOps("nameList2").range(0, 10);
        System.out.println(list);
    }

    /**
     * 按照索引查询
     */
    @Test
    public void searchByIndex() {
        String string=(String) redisTemplate.boundListOps("nameList2").index(1);
        System.out.println(string);
    }

    /**
     * 删除其中一个元素
     */
    @Test
    public void removeValue() {
        //单独的移除其中一个元素,第一个参数是移除的个数,不是位置的下表
        redisTemplate.boundSetOps("nameList1").remove(1,"关羽");
    }

    /**
     * 删除整个List集合
     */
    @Test
    public void delete() {
        //单独的移除其中一个元素,第一个参数是移除的个数,不是位置的下表
        redisTemplate.delete("nameList1");
    }

}

9.Hash类型操作

package test;

import java.util.List;
import java.util.Set;

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.SpringJUnit4Cla***unner;

@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class TestHash {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 存值
     */
    @Test
    public void setValue() {
        redisTemplate.boundHashOps("nameHash").put("a", "唐僧");
        redisTemplate.boundHashOps("nameHash").put("b", "悟空");
        redisTemplate.boundHashOps("nameHash").put("c", "八戒");
        redisTemplate.boundHashOps("nameHash").put("d", "沙僧");
    }

    /**
     * 取所有key
     */
    @Test
    public void getKey() {
        Set keys=redisTemplate.boundHashOps("nameHash").keys();
        System.out.println(keys);
    }

    /**
     * 取所有value
     */
    @Test
    public void getValue() {
        List list=redisTemplate.boundHashOps("nameHash").values();
        System.out.println(list);
    }

    /**
     * 根据key取值
     */
    @Test
    public void searchValueByKey() {
        String string=(String) redisTemplate.boundHashOps("nameHash").get("b");
        System.out.println(string);
    }

    /**
     * 移除某个小key的值
     */
    @Test
    public void removeValueByKey() {
        redisTemplate.boundHashOps("nameHash").delete("c");
    }

    /**
     * 删除其中一个元素
     */
    @Test
    public void removeValue() {
        //单独的移除其中一个元素,第一个参数是移除的个数,不是位置的下表
        redisTemplate.boundSetOps("nameList1").remove(1,"关羽");
    }

    /**
     * 删除整个List集合
     */
    @Test
    public void delete() {
        //单独的移除其中一个元素,第一个参数是移除的个数,不是位置的下表
        redisTemplate.delete("nameList1");
    }

}

10.项目的目录结构

Spring Data Redis的使用方法
以上就是Spring Data Redis的使用方法,详细使用情况还得要大家自己使用过才能知道具体要领。如果想阅读更多相关内容的文章,欢迎关注创新互联行业资讯频道!


当前标题:SpringDataRedis的使用方法
转载注明:http://bjjierui.cn/article/ihcesi.html

其他资讯