一篇很好的入门学习文章: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. number_format

    number_format — 以千位分隔符方式格式化一个数字 说明 string number_format ( float $number [, int $decimals = 0 ] ) str ...

  2. Ubuntu 14.04 中安装 VMware10 Tools工具

    Run: apt-get install dkms linux-headers-$(uname -r) build-essential psmisc2 - Run: git clone https:/ ...

  3. 数据库批量修改表名,增加前缀(SQL server)

    exec sp_msforeachtable @command1=' declare @o sysname,@n sysname select @o=''?'' ,@n=stuff(@o,1,7,'' ...

  4. 【leetcode❤python】26. Remove Duplicates from Sorted Array

    #-*- coding: UTF-8 -*-class Solution(object):    def removeDuplicates(self, nums):        "&quo ...

  5. android测试参考,及CreateProcess failure, error问题解决

    今天小伙伴问我问题,我给了这2个小命令,或许做android测试的同学可以用得着. 截图命令adb shell /system/bin/screencap -p /sdcard/screenshot. ...

  6. SecureCRT显示中文和语法高亮

    因为默认情况下,SecureCRT不能显示语法高亮特性,整个界面颜色单一,看起来不爽,也没有效率,所有通过设置一下语法高亮还是很有必要的, 默认字体也看着不是很清晰,还是更改为我比较喜欢的Courie ...

  7. python利用xmlrpc方式对odoo数据表进行增删改查操作

    # -*- encoding: utf-8 -*- import xmlrpclib #导入xmlrpc库,这个库是python的标准库. username ='admin' #用户登录名 pwd = ...

  8. 省市县distpicker的使用

    下载地址https://github.com/fengyuanchen/distpicker 1.引入 <!-- 引入地址 begin --> <script type=" ...

  9. Ruby方法

    Ruby 方法 Ruby 方法与其他编程语言中的函数类似.Ruby 方法用于捆绑一个或多个重复的语句到一个单元中. 方法名应以小写字母开头.如果您以大写字母作为方法名的开头,Ruby 可能会把它当作常 ...

  10. BestCoder Valentine's Day Round

    昨晚在开赛前5分钟注册的,然后比赛刚开始就掉线我就不想说了(蹭网的下场……),只好用手机来看题和提交,代码用电脑打好再拉进手机的(是在傻傻地用手机打了一半后才想到的办法). 1001,也就是 hdu ...