美文网首页
非关系型数据库Redis + Spring整合Redis

非关系型数据库Redis + Spring整合Redis

作者: writeanewworld | 来源:发表于2017-12-14 21:57 被阅读0次

1.简介
数据库持久化数据主要是面向磁盘,磁盘的读写速度是比较慢的。在互联网中往往存在大量的数据请求,这种数据库承受不住高并发访问,服务器易宕机。
为了克服这些问题。javaweb往往引用NoSql技术。 NoSql(Redis MongoDb),是一种基于内存的简易数据库,提供一定的持久化功能。性能优越支持集群,分布式。有一定的事务能力,所以在高并发的情况下也可以保证数据的安全、一致。

2.安装Redis,写一个小程序简单测试一下电脑性能

下载Redis安装配置、新建java项目,导入jar包。

  package com.begin21.PerformanceTest;

import redis.clients.jedis.Jedis;

public class Test {
// 首先开启本机的Redis服务 redis-server.exe redis.windows.conf redis-cli.exe

public static void main(String[] args) {
    //链接redis
    Jedis jedis = new Jedis("localhost",6379);
    //记录操作次数
    int count = 0;
    //密码
    //jedis.auth("");
    try {
        //开始毫秒数
        long start = System.currentTimeMillis();
        while(true){
            //结束毫秒数
            long end = System.currentTimeMillis();
            //大于1秒结束操作
            if((end-start) >= 1000){
                break;
            }
            count++;
            jedis.set("test"+count , count+"");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }finally{
         //关闭数据库链接
        jedis.close();
    }
    System.out.println("redis每秒操作" + count + "次");
}}

性能测试:


image.png

只能说,贼垃圾

3Sping+Redis.
Redis中只提供了对String的操作,java以类对象为主,需要redis存储的字符串和java对象相互转换。
Spring对此进行了封装和支持,提供序列化的设计框架和一些序列化的类,通过序列化把java对象转化,redis存储起来。读取时,再由序列化过的字符串转化为对象。

4.步骤
配置数据库连接池
配置Spring提供的连接工厂
对象序列化
同一redis连接

配置文件:applicationContext.xml

 <?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" 
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">

<!-- 配置JedisPoolConfig连接池对象 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <!-- 配置最大空闲数 -->
    <property name="maxIdle" value="50" />
    <!-- 配置最大连接数 -->
    <property name="maxTotal" value="100" />
    <!-- 配置最大等待时间 -->
    <property name="maxWaitMillis" value="20000" />
</bean>

<!-- 配置JedisConnectionFactory spring提供的链接工厂-->
<bean id="connectionFactory"
    class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <property name="hostName" value="localhost"/>
    <property name="port" value="6379"/>
    <property name="poolConfig" ref="poolConfig"/>
</bean>

 <!-- 对象序列化 -->
 <!-- 键序列器 -->
 <bean id="stringRedisSerializer"
    class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
 <!-- 值序列器 -->
 <bean id="jdkSerializationRedisSerializer"
    class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> 

 <!--  同一Redis连接-->
 <bean id="redisTemplate"
    class="org.springframework.data.redis.core.RedisTemplate">
    <property name="connectionFactory" ref="connectionFactory"/>
    <property name="keySerializer" ref="stringRedisSerializer"/>
    <property name="valueSerializer" ref="jdkSerializationRedisSerializer"/>
</bean>
</beans>

Main:

  package com.begin21.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;

import com.begin21.pojo.Role;

public class RoleMain {

private static ApplicationContext applicationContext;

public static void main(String[] args) {
    testRedisl();
}

@SuppressWarnings("unchecked")
public static void testRedisl() {
    applicationContext = new ClassPathXmlApplicationContext(
            "applicationContext.xml");

    // 反射获取jar包中的RedisTemplate
    @SuppressWarnings({ "rawtypes" })
    RedisTemplate redisTemplate = applicationContext
            .getBean(RedisTemplate.class);

    Role role = new Role();
    role.setId(1);
    role.setRoleName("王凯");
    role.setNote("wangkai");

    // 序列化放到redis中
    redisTemplate.opsForValue().set("role1", role);
    SessionCallback<Role> callback = new SessionCallback<Role>() {

        @Override
        public Role execute(
                @SuppressWarnings("rawtypes") RedisOperations ops)
                throws DataAccessException {
            ops.boundValueOps("role1").set(role);
            return (Role) ops.boundValueOps("role1").get();
        }
    };
    Role savedRole = (Role) redisTemplate.execute(callback);
    System.out.println(savedRole.getRoleName()+savedRole.getNote());
}
}

相关文章

  • redis总结

    Redis Redis是什么 开源免费,key-value的非关系型数据库 关系型数据库与非关系型数据库的区别? ...

  • 带你进spring-集成redis

    Redis是key-value存储的非关系型数据库。Spring Data Redis包含了多个模板实现,用来完成...

  • Redis基础

    非关系型数据库 NoSQL Nosql特点 Redis Redis 简介 Redis 优势 Redis 安装 Wi...

  • 初识Redis(一):Redis简介及数据类型

    Redis简介 Redis是一款开源免费、高性能的非关系型数据库 非关系型数据库(Not Only SQL): 简...

  • SpringBoot 整合 Redis,一篇解决“缓存”的所有问

    前言 这篇博文我们介绍SpringBoot如何整合Redis来访问非关系型数据库,带你深入了解Redis的自动原理...

  • 非关系型数据库Redis + Spring整合Redis

    1.简介数据库持久化数据主要是面向磁盘,磁盘的读写速度是比较慢的。在互联网中往往存在大量的数据请求,这种数据库承受...

  • Python操作MySQL(待续)

    数据库分类框架 关系型数据库:Mysql,Oracle,Sqlite 非关系型数据库:MongoDB,Redis非...

  • Redis入门

    Redis入门 概念:redis是一款高性能的nosql(非关系型)的数据库。 关系型数据库:mysql、orac...

  • Redis

    Redis 关系型和非关系数据库比较: redis数据结构 redis列表数据结构 案例 概念: redis是一款...

  • 常用的一些工具

    关系型数据库 Mysql Pg Oracle Tidb 非关系型数据库 redis mongo memcach 注...

网友评论

      本文标题:非关系型数据库Redis + Spring整合Redis

      本文链接:https://www.haomeiwen.com/subject/rnrlwxtx.html