一篇很好的入门学习文章:Redis学习

Redis是一种nosql数据库,在开发中常用做缓存。

1、下载地址:

  低版本下载地址:https://github.com/dmajkic/redis/downloads

  高版本下载地址:https://github.com/MSOpenTech/redis/releases

  还可以下载 RedisStudio.exe来连接redis和查看redis数据。

2、把内容解压到到需要安装的目录下,比如:D:\redis2.4.5

3、运行cmd窗口 切换到D:\redis2.4.5目录下

4、启动redis服务:运行 redis-server.exe redis.conf

  redis.conf 是redis服务器的配置文件

D:\redis2.4.5>redis-server.exe redis.conf
[6540] 02 Nov 16:44:23 * Server started, Redis version 2.4.5
[6540] 02 Nov 16:44:23 # Open data file dump.rdb: No such file or directory
[6540] 02 Nov 16:44:23 * The server is now ready to accept connections on port
[6540] 02 Nov 16:44:24 - 0 clients connected (0 slaves), 1179896 bytes in use
[6540] 02 Nov 16:44:29 - 0 clients connected (0 slaves), 1179896 bytes in use
[6540] 02 Nov 16:44:34 - 0 clients connected (0 slaves), 1179896 bytes in use

出现以上信息说明Redis服务端已经安装成功

5、 操作redis
重新打开一个cmd窗口,切换到D:\redis2.4.5目录下
运行 redis-cli.exe -h 127.0.0.1 -p 6379
其中 127.0.0.1是本地ip,6379是redis服务端的默认端口。

D:\redis2.4.5>redis-cli.exe -h 127.0.0.1 -p 33840
redis 127.0.0.1:6379>

出现以上信息说明已经连接上redius服务器
Redis windows环境下搭建已经完成

6、测试

#定义test值为yeqing
redis 127.0.0.1:6379> set test "yeqing"
OK

redis 127.0.0.1:6379> get test
"yeqing"

#修改test值为test
redis 127.0.0.1:6379> set test "test"
OK

redis 127.0.0.1:6379> get test
"test"

7、java中使用redis来存储session

搭配:redis2.8以上版本(兼容spring-session-.0.1) + jedis-2.7.0.jar + spring-data-redis-1.5.0.jar + spring-session-1.0.1.jar

Jedis是Redis在java中的redis - client

注意:千万不要使用jedis-2.7.2  来搭配 ,版本太高会报空指针错误

spring-data-redis是1.5的版本,jedis是2.7.2的版本,jedis版本太高错误在这:

A. 在 spring-data-redis的JedisConnectionFactory 中

SEND_COMMAND = ReflectionUtils.findMethod(
  Connection.class,
  "sendCommand",
  new Class[] { Command.class, byte[][].class }
);

ReflectionUtils.makeAccessible(SEND_COMMAND);

B. 版本差异

redis 2.7. 的connection类 :

protected Connection sendCommand(final Command cmd, final byte[]... args)

而2..2版本以变:

protected Connection sendCommand(final ProtocolCommand cmd, final byte[]... args)

解析:
由于2.7.2版本中使用 ProtocolCommand 而不是 Command
所以SEND_COMMAND 的值为null

然后执行
ReflectionUtils.makeAccessible(SEND_COMMAND);
当然是报出空指针啦

C. 报错如下

下面展开存储session的正确姿势

1) web.xml

  <!-- 将这个filter配置在任何filter之前 -->
  <filter>
<filter-name>springSessionRepositoryFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSessionRepositoryFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

2) 新建 applicationContext-session.xml 并 import 到spring配置中,编写内容如下:

<import resource="classpath*:applicationContext-session.xml" />

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 1、对象池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxTotal}"/><!-- 控制一个pool可分配多少个jedis实例 -->
<property name="maxIdle" value="${redis.pool.maxIdle}" /><!-- 控制一个pool最多有多少个状态为空闲的jedis实例 -->
<property name="minIdle" value="${redis.pool.minIdle}"/>
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" /> <!-- 表示当borrow一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException -->
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" /> <!-- 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的 -->
<property name="testOnReturn" value="${redis.pool.testOnReturn}"/>
<property name="testWhileIdle" value="${redis.pool.testWhileIdle}"/>
</bean> <!-- 2、工厂实现 -->
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
destroy-method="destroy">
<property name="hostName" value="${redis.ip}" />
<property name="port" value="${redis.port}" />
<property name="timeout" value="${redis.timeout}" />
<property name="database" value="${redis.database}" />
<property name="usePool" value="${redis.usePool}" />
<property name="poolConfig" ref="jedisPoolConfig" />
</bean> <!-- 3、操作模板类 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
</bean>

  <!-- 4、 将session放入redis-->
<bean id="redisHttpSessionConfiguration"
class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="${session.maxInactiveIntervalInSeconds}" />
</bean> </beans>

3) 新建 redis.properties,写入如下内容:

<value>classpath*:redis.properties</value>

##redis basic config
redis.pool.maxTotal=1024
redis.pool.maxIdle=200
redis.pool.minIdle=1
redis.pool.maxWaitMillis=10000 redis.pool.testOnBorrow=true
redis.pool.testOnReturn=true
redis.pool.testWhileIdle=true redis.usePool=true redis.timeout=15000
redis.database=0 #single pool server
redis.ip=127.0.0.1
redis.port=6379 # session invalidate time
session.maxInactiveIntervalInSeconds = 1800

[redis] windwos下安装和使用redis的更多相关文章

  1. Redis linux 下安装 及扩展配置

    1.首先在/usr/local/ 创建文件夹 reids Cd /usr/local/ mkdir redis 2.把redis安装包放在redis目录下面进行解压phpredis-2.2.4.tar ...

  2. windows下安装和配置redis

    1.windows下安装和配置redis 1.1 下载: 官网(linux下载地址):https://redis.io/ Windows系统下载地址:https://github.com/MSOpen ...

  3. Redis linux 下安装

    Redis linux 下安装 下载Redis安装包,可以从Redis中文网站中下载 下载地址:http://www.redis.cn/download.html Redis4.0 稳定版本 使用&l ...

  4. linux下安装php扩展redis缓存

    下载phpredis安装包 wget https://github.com/nicolasff/phpredis/tarball/master 在下载目录解压phpredis.tar.gz tar z ...

  5. linux系统下安装单台Redis

    注意:搭建redis前一定要安装gcc redis安装方式一 1.安装gcc命令:yum install -y gcc #安装gcc [root@localhost src]# yum install ...

  6. Redis Windows下安装方法

    一.安装 首先在网上下载Redis,下载地址:https://github.com/MicrosoftArchive/redis/releases 根据电脑系统的实际情况选择32位还是64位,在这里我 ...

  7. linux下安装与配置Redis

    1.安装 (1)获取源代码 wget http://download.redis.io/releases/redis-4.0.8.tar.gz (2)解压 tar xzvf redis-4.0.8.t ...

  8. linux下安装与部署redis

    一.Redis介绍 Redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统.和Memcache类似,但很大程度补偿了Memcache的不足,它支持存储的value类型相对更多 ...

  9. CentOS下安装JDK,Tomcat,Redis,Mysql,及项目发布

    上传文件到服务器,安装lrzsz , 可以将本地的文件上传到linux系统上. 如果是CentOS则可以用yum install lrzsz 命令安装,更方便. 或:yum -y install lr ...

随机推荐

  1. SVN Unable to connect to a repository at URL问题解决

    图1 之前用的好好的,不知道为什么今天就不行了,根据网上给的方法TortoiseSVN -> Settings -> Saved Data,点击个个"Clear"按钮, ...

  2. Cheatsheet: 2013 07.09 ~ 07.20

    Mobile How to implement Android Splash Screen Migrating iOS MVC Applications to Windows Phone 8 (M-V ...

  3. [51NOD1095] Anigram单词(map)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1095 字典的单词在map中排序和不排序各存1次,查的时候相减. ...

  4. Hibernate+Struts2+jsp 修改用户信息

    在用户列表页面点击修改,进入修改页面 修改薪酬为555,点击提交,重新跳回该页面 修改成功 关键代码如下 基层的代码,这里增加了一个根据用户id查询的方法 dao层 //修改 public USer ...

  5. windows c dll的创建与调用

    DLL代码: // TestDynamic.cpp: implementation of the TestDynamic class. // ///////////////////////////// ...

  6. [SAP ABAP开发技术总结]BAPI调用

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  7. CUBRID学习笔记 9 创建示例数据库

    如果你安装的时候没有装数据库,可以后期装 其实和上文基本差不多. cd ~ mkdir CUBRID_databases cd CUBRID_databases mkdir demodb cd dem ...

  8. Binding的源与路径

    1.把控件作为Binding的源 例子:拖动Slider,输入框中的值也会跟着改变,或在输入框中输入数值,滑动条也会自动移动 <Window x:Class="把控件作为Binding ...

  9. 关于js运动的一些总结

    js运动实现,有两种.一种是速度版,另一种是时间版. 速度版是通过对速度的加减乘除,得出元素的运动数据.时间版是通过对时间进行Tween公式运算,得出元素的运动数据. 速度版运动优点:容易在运动过程中 ...

  10. win10系统更新补丁时进度条一直卡在0%不动的解决方案

    为了能够让win10系统更加安全稳定,很多用户都会时不时为自己的电脑安装补丁.不过,部分用户在为win10系统更新补丁时,却会遇到进度条一直卡在0%不动的问题.这该怎么办呢?下面,小编就告诉大家解决该 ...