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
去掉行前的注释,并修改密码为所需的密码,保存文件
进入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); } }
本文参照