redis-cluster和Spring集成,基于Cache注解

【参考文章】http://blog.csdn.net/fengyong7723131/article/details/52995592

前言:

redis-cluster实现了缓存分布式和备份,搭建起来特别简单,没搭建的可以看一下redis-cluster集群搭建这篇博客,下面是一些主要的配置文件,如果复制跑不起来的朋友可以直接下载我搞好的完整项目,是一个maven项目,下载地址:http://download.csdn.net/detail/qq_34021712/9822630


pom.xml


  4.0.0
  com.redis
  redis-spring
  0.0.1-SNAPSHOT
  war
  
	
		
		
			joda-time
			joda-time
			2.5
		
		
		
			org.apache.commons
			commons-lang3
			3.3.2
		
		
			org.apache.commons
    		commons-io
    		1.3.2
		
		
			commons-net
			commons-net
			3.3
		
		
		
			com.fasterxml.jackson.core
			jackson-databind
			2.4.2
		
		
		
			org.apache.httpcomponents
			httpclient
			4.3.5
		
		
		
			junit
			junit
			4.12
			test
		
		
		
			org.slf4j
			slf4j-log4j12
			1.6.4
		
		
				org.springframework
				spring-context
				4.2.6.RELEASE
			
			
				org.springframework
				spring-beans
				4.2.6.RELEASE
			
			
				org.springframework
				spring-webmvc
				4.2.6.RELEASE
			
			
				org.springframework
				spring-jdbc
				4.2.6.RELEASE
			
			
				org.springframework
				spring-aspects
				4.2.6.RELEASE
			
			
			
				javax.servlet
				servlet-api
				2.5
				provided
			
			
				javax.servlet
				jsp-api
				2.0
				provided
			
			
			
				redis.clients 
				jedis
				2.9.0
			
			 
	          
			  org.springframework.data  
			  spring-data-redis  
			  1.8.1.RELEASE  
			
			
			
				org.mybatis
				mybatis
				3.2.8
			
			
				org.mybatis
				mybatis-spring
				1.2.2
			
			
				com.github.miemiedev
				mybatis-paginator
				1.2.15
			
			
			
				mysql
				mysql-connector-java
				5.1.32
			
			
			
				com.alibaba
				druid
				1.0.9
			
	
  
  	
  		
  			maven-war-plugin
  			
  				3.0
  			
  		
  		
			
				org.apache.maven.plugins
				maven-compiler-plugin
				3.2
				
					1.7
					1.7
					UTF-8
				
			
  	
  


applicationContext-dao.xml





	
	
	
	
	
		
		
		
		
		
		
	
	
	
		
		
	
	
	
		
	

applicationContext-service.xml





	
	
	
	
		
		
	
	
	
		
			
			
			
			
			
			
			
			
			
			
		
	
	
	
		
	
	

applicationContext-redis.xml
  



	    
    
	
	  
      
          
    
    
      
      
          
          
          
          
          
          
    
    
      
      
          
          
    
	
	  
      
          
      
    
      
      
         
          
          
          
      
      
          
          
          
              
          
          
              
          
          
              
          
          
              
          
      
    
    
        
         
             
                
                  
                    
                         
                         
                    
                
             
     
	

web.xml


	redis-spring
	
  
      log4j
      classpath:log4j.properties
  
	
		index.html
		index.htm
		index.jsp
		default.html
		default.htm
		default.jsp
	
	
	
		contextConfigLocation
		classpath:spring/applicationContext-*.xml
	
	
		org.springframework.web.context.ContextLoaderListener
	
	
	
		CharacterEncodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			utf-8
		
	
	
		CharacterEncodingFilter
		/*
	
	
	
		redis-spring
		org.springframework.web.servlet.DispatcherServlet
		
		
			contextConfigLocation
			classpath:spring/springmvc.xml
		
		1
	
	
		redis-spring
		/rest/*
	

RedisCache.java
package com.redis.redis;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;




public class RedisCache implements Cache {


	private RedisTemplate redisTemplate;
	private String name;


	public RedisTemplate getRedisTemplate() {
		return redisTemplate;
	}


	public void setRedisTemplate(RedisTemplate redisTemplate) {
		this.redisTemplate = redisTemplate;
	}


	public void setName(String name) {
		this.name = name;
	}


	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return this.name;
	}


	@Override
	public Object getNativeCache() {
		// TODO Auto-generated method stub
		return this.redisTemplate;
	}


	@Override
	public ValueWrapper get(Object key) {
		// TODO Auto-generated method stub
		final String keyf = (String) key;
		Object object = null;
		object = redisTemplate.execute(new RedisCallback() {
			public Object doInRedis(RedisConnection connection)
					throws DataAccessException {


				byte[] key = keyf.getBytes();
				byte[] value = connection.get(key);
				if (value == null) {
					return null;
				}
				return toObject(value);


			}
		});
		return (object != null ? new SimpleValueWrapper(object) : null);
	}


	@Override
	public void put(Object key, Object value) {
		// TODO Auto-generated method stub
		final String keyf = (String) key;
		final Object valuef = value;
		final long liveTime = 86400;


		redisTemplate.execute(new RedisCallback() {
			public Long doInRedis(RedisConnection connection)
					throws DataAccessException {
				byte[] keyb = keyf.getBytes();
				byte[] valueb = toByteArray(valuef);
				connection.set(keyb, valueb);
				if (liveTime > 0) {
					connection.expire(keyb, liveTime);
				}
				return 1L;
			}
		});
	}


	/**
	 * 描述 : . 
*

* <使用方法说明> *

* * @param obj * @return */ private byte[] toByteArray(Object obj) { byte[] bytes = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(obj); oos.flush(); bytes = bos.toByteArray(); oos.close(); bos.close(); } catch (IOException ex) { ex.printStackTrace(); } return bytes; } /** * 描述 : .
*

* <使用方法说明> *

* * @param bytes * @return */ private Object toObject(byte[] bytes) { Object obj = null; try { ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bis); obj = ois.readObject(); ois.close(); bis.close(); } catch (IOException ex) { ex.printStackTrace(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } return obj; } @Override public void evict(Object key) { // TODO Auto-generated method stub final String keyf = (String) key; redisTemplate.execute(new RedisCallback() { public Long doInRedis(RedisConnection connection) throws DataAccessException { return connection.del(keyf.getBytes()); } }); } @Override public void clear() { // TODO Auto-generated method stub redisTemplate.execute(new RedisCallback() { public String doInRedis(RedisConnection connection) throws DataAccessException { connection.flushDb(); return "ok"; } }); } @Override public T get(Object key, Class type) { // TODO Auto-generated method stub return null; } @Override public ValueWrapper putIfAbsent(Object key, Object value) { // TODO Auto-generated method stub return null; } }
Controller层和mapper层接口什么的都不在这里说明了,只是查询一下数据库,直接粘贴serviceImpl类
package com.redis.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;


import com.redis.mapper.StudentMapper;
import com.redis.pojo.Student;
import com.redis.service.StudentService;


@Service
public class StudentServiceImpl implements StudentService {
	
	@Autowired
	private StudentMapper studentMapper;


	@Override
	public int insert(Student student) {
		System.out.println("插入方法执行了########");
		return studentMapper.insert(student);
	}


	@Override
	@CacheEvict(value="defaultCache",key="'student_id_'+#id")
	public int deleteByPrimaryKey(Long id) {
		System.out.println("删除方法执行了########");
		return studentMapper.deleteByPrimaryKey(id);
	}


	@Override
	@CachePut(value="defaultCache",key="'student_id_'+#student.id")
	public Student updateByPrimaryKey(Student student) {
		System.out.println("修改方法执行了########");
		studentMapper.updateByPrimaryKey(student);
		return student;
	}


	@Override
	@Cacheable(value="defaultCache",key="'student_id_'+#id")//#id
	public Student selectByPrimaryKey(Long id) {
		System.out.println("查询方法执行了########");
		return studentMapper.selectByPrimaryKey(id);
	}


}

测试已通过

你可能感兴趣的:(redis)