Redis windows环境安装 以及 redis整合spring
Redis对于Linux是官方支持的,安装和使用没有什么好说的,普通使用按照官方指导,5分钟以内就能搞定。详情请参考:
Redis官方是不支持windows的,只是 Microsoft Open Tech group 在 GitHub上开发了一个Win64的版本,项目地址是:
https://github.com/MSOpenTech/redis
打开以后,可以直接使用浏览器下载,或者Git克隆。
(注意: dist文件改变了下载地址: https://github.com/MSOpenTech/redis/releases )
在 Release 页面中,可以找到 msi 安装文件以及 .zip 文件(而且有3.0的beta版,请下拉查找)。
下载解压,没什么好说的,在解压后的bin目录下有以下这些文件:
- redis-benchmark.exe #基准测试
- redis-check-aof.exe # aof
- redis-check-dump.exe # dump
- redis-cli.exe # 客户端
- redis-server.exe # 服务器
- redis.windows.conf # 配置文件
redis配置密码
1.通过配置文件进行配置
windos方式安装的redis配置文件通常在redis.windows.conf中,打开配置文件找到
- #requirepass foobared
去掉行前的注释,并修改密码为所需的密码,保存文件
- requirepass wssjj123

进入redis目录下启动redis
redis-server.exe redis.windows.conf
这个时候尝试登录redis,发现可以登上,但是执行具体命令是提示操作不允许

尝试用密码登录并执行具体的命令看到可以成功执行

2.通过命令行进行配置
- redis 127.0.0.1:6379[1]> config set requirepass my_redis
- OK
- redis 127.0.0.1:6379[1]> config get requirepass
- 1) "requirepass"
- 2) "my_redis"
无需重启redis
使用第一步中配置文件中配置的老密码登录redis,会发现原来的密码已不可用,操作被拒绝
- redis-cli -h 127.0.0.1 -p 6379 -a myRedis
- redis 127.0.0.1:6379> config get requirepass
- (error) ERR operation not permitted
使用修改后的密码登录redis,可以执行相应操作
- redis-cli -h 127.0.0.1 -p 6379 -a my_redis
- redis 127.0.0.1:6379> config get requirepass
- 1) "requirepass"
- 2) "my_redis
尝试重启一下redis,用新配置的密码登录redis执行操作,发现新的密码失效,redis重新使用了配置文件中的密码
- sudo service redis restart
- Stopping redis-server: [ OK ]
- Starting redis-server: [ OK ]
- redis-cli -h 127.0.0.1 -p 6379 -a my_redis
- redis 127.0.0.1:6379> config get requirepass
- (error) ERR operation not permitted
- redis-cli -h 127.0.0.1 -p 6379 -a myRedis
- redis 127.0.0.1:6379> config get requirepass
- 1) "requirepass"
- 2) "myRedis"
redis整合Spring
1、引入jar包
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.1.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
2、配置bean
在application.xml加入如下配置
<context:property-placeholder location="classpath:redis.properties" />
<!-- jedis 配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<!-- redis服务器中心 -->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="poolConfig" />
<property name="port" value="${redis.port}" />
<property name="hostName" value="${redis.host}" />
<property name="password" value="${redis.pass}" />
<property name="timeout" value="${redis.maxWait}"></property>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="keySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
</bean>
<!-- cache配置 -->
<bean id="redisUtil" class="me.shijunjie.util.RedisUtil">
<property name="redisTemplate" ref="redisTemplate" />
</bean>
其中配置文件redis一些配置数据redis.properties如下:
# Redis settings
redis.host=123.206.228.200
redis.port=6379
redis.pass=******
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
3、一些工具类
(1)RedisUtil
上面的bean中,RedisUtil是用来缓存和去除数据的实例
package me.shijunjie.util; import java.io.Serializable;
import java.util.Set;
import java.util.concurrent.TimeUnit; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations; /**
* redis cache 工具类
*
*/
public final class RedisUtil {
private Logger logger = LoggerFactory.getLogger(RedisUtil.class);
private RedisTemplate<Serializable, Object> redisTemplate; /**
* 批量删除对应的value
*
* @param keys
*/
public void remove(final String... keys) {
for (String key : keys) {
remove(key);
}
} /**
* 批量删除key
*
* @param pattern
*/
public void removePattern(final String pattern) {
Set<Serializable> keys = redisTemplate.keys(pattern);
if (keys.size() > 0)
redisTemplate.delete(keys);
} /**
* 删除对应的value
*
* @param key
*/
public void remove(final String key) {
if (exists(key)) {
redisTemplate.delete(key);
}
} /**
* 判断缓存中是否有对应的value
*
* @param key
* @return
*/
public boolean exists(final String key) {
return redisTemplate.hasKey(key);
} /**
* 读取缓存
*
* @param key
* @return
*/
public Object get(final String key) {
Object result = null;
ValueOperations<Serializable, Object> operations = redisTemplate
.opsForValue();
result = operations.get(key);
return result;
} /**
* 写入缓存
*
* @param key
* @param value
* @return
*/
public boolean set(final String key, Object value) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate
.opsForValue();
operations.set(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
} /**
* 写入缓存
*
* @param key
* @param value
* @return
*/
public boolean set(final String key, Object value, Long expireTime) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate
.opsForValue();
operations.set(key, value);
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
} public void setRedisTemplate(
RedisTemplate<Serializable, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
}
4、执行结果:
写了一个简单的单元测试如下:
package me.shijunjie.testRedis; import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import me.shijunjie.util.RedisUtil; public class RedisTest { private Logger logger = LoggerFactory.getLogger(RedisTest.class); private ApplicationContext context = null;
private RedisUtil redisUtil; @Before
public void before() {
System.out.println("初始化spring容器");
context = new ClassPathXmlApplicationContext("applicationContext.xml");
redisUtil = (RedisUtil) context.getBean("redisUtil");
} @Test
public void testSet() {
boolean isset = redisUtil.set("test", "hh");
System.out.println(isset);
Object value = redisUtil.get("test");
System.out.println(value);
} }

本文参照
http://blog.csdn.net/evankaka/article/details/50396325
http://blog.csdn.net/zyz511919766/article/details/42268219
Redis windows环境安装 以及 redis整合spring的更多相关文章
- PHP XAMPP windows环境安装扩展redis 致命错误: Class 'Redis' not found解决方法
PHP XAMPP windows环境安装扩展redis 致命错误: Class 'Redis' not found解决方法 1.电脑需要先安装redis服务端环境,并在安装目录下打开客户端redis ...
- Redis Windows环境安装
1.下载Windows 版本 Redis: https://github.com/ServiceStack/redis-windows 2. 解压文件: F:\开源代码学习\01_Redis 打开 目 ...
- Redis——windows环境安装redis和redis sentinel部署
一:Redis的下载和安装 1:下载Redis Redis的官方网站Download页面,Redis提示说:Redis的正式版不支持Windows,要Windows学习Redis,请点击Learn m ...
- Redis:在windows环境安装Redis
Redis:在windows环境安装Redis 第一步: 下载windows版本的Redis:https://github.com/MSOpenTech/Redis. 第二步: 在命令行执行:D:\r ...
- Redis Windows环境搭建
简介 Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理.它支持字符串.哈希表.列表.集合.有序集合,位图,hyperloglogs等数据类型.内置复制 ...
- 【转】redis windows环境搭建
一.下载redis windows压缩包 地址参考: https://github.com/ServiceStack/redis-windows/tree/master/downloads https ...
- window安装reidis完成之后,想要把数据存入redis,必须开扩展,不然报错,redis windows phpstudy 安装扩展
redis windows phpstudy 安装扩展 1.http://windows.php.net/downloads/pecl/releases/redis/3.1.5rc1/ 2.htt ...
- Windows环境下启动Redis报错:Could not create server TCP listening socket 127.0.0.1:6379: bind: 操作成功完成。(已解决)
问题描述: 今天在windows环境下启动Redis时启动失败报错: 解决方案: ①运行命令:redis-cli.exe ②退出Redis ③运行命令:redis-server.exe redis.w ...
- Windows环境安装tesseract-ocr 4.00并配置环境变量
最近要做文字识别,不让直接用别人的接口,所以只能尝试去用开源的类库.tesseract-ocr是惠普公司开源的一个文字识别项目,通过它可以快速搭建图文识别系统,帮助我们开发出能识别图片的ocr系统.因 ...
随机推荐
- PostgreSQL Streaming Replication的FATAL ERROR
磨砺技术珠矶,践行数据之道,追求卓越价值回到上一级页面: PostgreSQL集群方案相关索引页 回到顶级页面:PostgreSQL索引页[作者 高健@博客园 luckyjackgao@gm ...
- 【JUC源码解析】FutureTask
简介 FutureTask, 一个支持取消行为的异步任务执行器. 概述 FutureTask实现了Future,提供了start, cancel, query等功能,并且实现了Runnable接口,可 ...
- TensorFlow深度学习实战---循环神经网络
循环神经网络(recurrent neural network,RNN)-------------------------重要结构(长短时记忆网络( long short-term memory,LS ...
- 【Jmeter测试】使用Java请求进行Dubbo接口的测试
如何构建一个Dubbo接口测试的通用框架(https://github.com/nitibu/jmeter-dubbo-test)从上面的流程我们可以看出,测试类大致的一个结构: 使用json文件来 ...
- Elasticsearch.Net 异常:[match] query doesn't support multiple fields, found [field] and [query]
用Elasticsearch.Net检索数据,报异常: )); ElasticLowLevelClient client = new ElasticLowLevelClient(settings); ...
- Prometheus+Grafana监控部署实践
参考文档: Prometheus github:https://github.com/prometheus grafana github:https://github.com/grafana/graf ...
- 【python 3.6】python获取当前时间及过去或将来的指定时间
最近有个查询api,入参需要一个startTime,一个endTime,刚好用到datetime. 留此记录. #python 3.6 #!/usr/bin/env python # -*- codi ...
- linux上open-vswitch安装和卸载
一. ovs 从源码编译安装: 安装依赖项: # apt-get install make # apt-get install gcc # apt-get install build-essentia ...
- DeepLearning - Overview of Sequence model
I have had a hard time trying to understand recurrent model. Compared to Ng's deep learning course, ...
- springjdbc使用c3p0连接池报错 java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector
MyMaincom.test.sunc.MyMaintestMethod(com.test.sunc.MyMain)org.springframework.beans.factory.BeanCrea ...