一篇很好的入门学习文章: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. 修改ftp密码

    1.运行cmd2.在DOS窗口中输入FTP 127.0.0.13.出现用户名输入提示“user”,键入用户名,按回车4.出现输入密码提示:“Password”,键入密码后按回车登录到服务器中5.在ft ...

  2. Linux 性能监测:Network

    网络的监测是所有 Linux 子系统里面最复杂的,有太多的因素在里面,比如:延迟.阻塞.冲突.丢包等,更糟的是与 Linux 主机相连的路由器.交换机.无线信号都会影响到整体网络并且很难判断是因为 L ...

  3. Exchange 2010 邮箱大小限制原则

    在 Exchange中文站 的QQ群(68280328)里经常会有朋友问到关于 Exchange 2010 邮件大小限制的问题,因为有许多地方,而且定义的内容又是同样的,所以,让本来很简单的限制原则变 ...

  4. Python串口编程

    python的串口网上有很多例子,这里了只是把认为好的整理到一起. 首先,应该安装serial模块,还能开始后续的操作.我用的python2.6,serial模块可以在这里下载安装serial模块下载 ...

  5. Q查询

    一.Complex lookups with Q objects(Q对象的复杂查询) 仅仅靠单一的关键字参数查询已经很难满足查询要求.此时Django为我们提供了Q查询: class Q 1.Q对象( ...

  6. SELECT时为何要加WITH(NOLOCK)

    此文章非原创,仅为分享.学习!! 要提升SQL的查询效能,一般来说大家会以建立索引(index)为第一考虑.其实除了index的建立之外,当我们在下SQL Command时,在语法中加一段WITH ( ...

  7. ALV详解:Function ALV(一)

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

  8. Shell Sort(草稿)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Shel ...

  9. NYOJ 93 汉诺塔(三)

    汉诺塔(三) 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 在印度,有这么一个古老的传说:在世界中心贝拿勒斯(在印度北部)的圣庙里,一块黄铜板上插着三根宝石针.印度 ...

  10. iOS - KVC 键值编码

    1.KVC KVC 是 Key-Value Coding 的简写,是键值编码的意思,属于 runtime 方法.Key Value Coding 是 cocoa 的一个标准组成部分,是间接给对象属性设 ...