jedis操作set_使用Redis来进行缓存操作

189a1d87edc648c6ded7b6c3c1fc0109.png

在今日,发现有部分功能因SQL语句查询过于缓慢,并且无法在进行优化(由于部分原因导致SQL固定,数据库结构无法改变,外键已经无法设置),但是在我尽力压缩时间后每一次的查询也是有7-8s的时间,这是绝对无法接受的,因此我想到使用Redis进行远程读写来实现一个伪缓存的操作。

在上篇文章中我已经讲过,Redis是基于内存的NOSQL数据库,拥有每秒十万级别的读写能力,因此用它来实现非常完美,具体步骤如下:

1.安装redis数据库到虚拟机(服务器)中,详情见上篇文章

2.在项目中进行远程调用:

①.笔者比较喜欢使用maven作为包依赖,所以在这里直接讲解maven的做法:

首先导包

<!--redis连接所需--><!--redis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency><!-- config redis data and client jar-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.0.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency><!--JSON转化工具-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency><!--end-->

这里的包除了最后一个,其它都是必须的!!!

②.创建redis-context.xml配置文件,配置连接池,连接工厂,kv序列化器以及redis模板对象(PS:这里可以不用redis模板对象也可以,但是就和我们的SpringCloud一样,不可能上手直接Feign来远程调用,一般都是用模板调用来学习)

<?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" 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-4.3.xsd"><!-- 连接池配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"><!-- 最大空闲数 -->
<property name="maxIdle" value="50"></property><!-- 最大连接数 -->
<property name="maxTotal" value="100"></property><!-- 最大等待时间 -->
<property name="maxWaitMillis" value="20000"></property>
</bean><!-- 配置连接工厂 -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="192.168.3.139"></property>
<property name="port" value="6379"></property>
<property name="password" value="root"></property>
<property name="poolConfig" ref="poolConfig"></property>
</bean><!-- 配置 key 和 value 的序列化器 -->
<bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/><!-- 配置Redis模板对象 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="keySerializer" ref="stringRedisSerializer"></property>
<property name="valueSerializer" ref="jdkSerializationRedisSerializer"></property>
</bean>
</beans>

这里的一些基本信息是外置比较好,当然,内置也行。

③.在spring的核心xml配置文件中进行导入:

<import resource="classpath:redis-context.xml"></import>

④.测试:

@Autowiredprivate RedisTemplate redisTemplate;

@RequestMapping(value = "redis",method = RequestMethod.GET)public void redis(){
System.out.println("成功进入到redis末班中");redisTemplate.opsForValue().set("hello","world");
System.out.println("redis设值语句执行成功!");
}

成功输出语句即完成成功!

如果失败的话大概率是你redis的服务没有开启或者你的防火墙设置外部不允许连接,鹏大道这种问题请查看笔者的上一篇文章,处理方法有写过,比较简单,都是操作redis目录中的redis.conf配置文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值