您的位置:首页 > 数据库 > Redis

Redis安装+spring注解集成Redis

2017-04-27 14:58 85 查看

Redis 安装

     一、windows 下安装

 下载地址:http://https://github.com/MSOpenTech/redis/releases

打开一个cmd窗口,使用cd命令切换到文件目录E:\redis,运行redis-server.exe redis.windows.conf。后面那个redis.windows.conf可以省略,如果省略,会启用默认的。输入之后,会显示如下界面:



这时候另起一个cmd窗口,原来的不要关闭,不然就无法访问服务端了。

切换到redis目录下运行redis-cli.exe -h 127.0.0.1 -p 6379

设置键值对 set myKey abc

取出键值对 get myKey

这个时候尝试登陆redis,发现可以登录,但是执行具体命令提示操作不允许,因为需要密码登录,命令如下:



修改密码 CONFIG   set   requirepass "123456",下载redis客户端http://download.csdn.net/detail/l578854269/9827566运行

   二、linux下安装

下载地址:http://redis.io/download,下载最新文档版本并安装:

$ wget http://download.redis.io/releases/redis-2.8.17.tar.gz $ tar xzf redis-2.8.17.tar.gz
$ cd redis-2.8.17
$ make


make完后 redis-2.8.17目录下会出现编译后的redis服务程序redis-server,还有用于测试的客户端程序redis-cli,两个程序位于安装目录 src 目录下:

下面启动redis服务.

$ cd src
$ ./redis-server

启动redis服务进程后,就可以使用测试客户端程序redis-cli和redis服务交互了。比如:

$ cd src
$ ./redis-cli
redis> set foo bar
OK
redis> get foo
"bar"


  三、spring注解集成

 pom.xml导入相关jar包

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.0.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>


Redis配置类:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@PropertySource(value = { "classpath:redis.properties" })
public class RedisConfig {

@Autowired
private Environment env;

public JedisConnectionFactory redisConnectionFactory() {
JedisConnectionFactory cf = new JedisConnectionFactory();
cf.setHostName(env.getProperty("redis.host"));
cf.setPort(Integer.valueOf(env.getProperty("redis.port")));
cf.setPassword(env.getProperty("redis.password"));
cf.afterPropertiesSet();
return cf;

}

@Bean
public RedisTemplate<String, String> redisTemplate() {
RedisTemplate<String, String> redis = new RedisTemplate<>();
redis.setConnectionFactory(redisConnectionFactory());
// 设置 key value 序列号类型
redis.setValueSerializer(stringRedisSerializer());
redis.setHashValueSerializer(stringRedisSerializer());
redis.afterPropertiesSet();
return redis;
}

@Bean
public StringRedisSerializer stringRedisSerializer() {
return new StringRedisSerializer();
}

@Bean
public StringRedisTemplate stringRedisTemplate() {
return new StringRedisTemplate(redisConnectionFactory());
}

@Bean
public CacheManager cacheManager() {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate());
cacheManager.setDefaultExpiration(3000); // Sets the default expire time
return cacheManager;
}
}


接口实现:
@Autowired
private StringRedisTemplate stringRedisTemplate;

private Jedis jedis;

private Jedis getJedis() {
if (jedis == null) {
jedis = (Jedis) stringRedisTemplate.getConnectionFactory().getConnection().getNativeConnection();
return jedis;
}
return jedis;
}

//接口测试
public void test(){
//添加缓存数据Map
Map<String, String> map = new HashMap<String, String>();
map.put("ip", server.getIpAddress());
map.put("port", server.getPort());
map.put("links", String.valueOf(server.getLinks()));
getJedis().hmset(server.getIpAddress(), map);
//获取所有的key<br>
Set<String> keys = getJedis().keys("*");
Iterator<String> values = keys.iterator();
while (values.hasNext()) {
String value = values.next();
System.out.println(value);
}

//获取缓存数据并更新数据<br>
String key = "123456";
Map<String, String> valueMap = getJedis().hgetAll(key);
valueMap.put("links", "2");
getJedis().hmset(key, valueMap);
}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  redis redisClient