Redis安装+spring注解集成Redis

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); 
	}


 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值