`
huangzixun
  • 浏览: 67455 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

使用spring整合SSM实现注解式的缓存系统

阅读更多

近期公司的产品重构,底层使用了memcached 作为缓存系统,使用了几天之后发现每次增删查改之后都要手动进行更新缓存,为了偷懒,在网上查了查,发现使用SSM开源项目可以很方便的通过几个注解进行管理缓存。其实原理也很简单,主要利用了spring 的AOP编程,现在简单介绍下是如何实现的。

首先,在你的项目工程的pom.xml文件中添加以下依赖,ssm针对不同的memcached 客户端需要添加不同的实现

这是针对xmemcached 客户端
<dependency>
		<groupId>com.google.code.simple-spring-memcached</groupId>
		<artifactId>xmemcached-provider</artifactId>
		<version>3.1.0</version>
	</dependency>
这是针对spymemcached客户端
	<dependency>
		<groupId>com.google.code.simple-spring-memcached</groupId>
		<artifactId>spymemcached-provider</artifactId>
		<version>3.1.0</version>
	</dependency>



这是我的数据库实体类:HUser.java

package com.mailbill.datacore.entity;

import java.io.Serializable;
import java.util.Date;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;

import com.google.code.ssm.api.CacheKeyMethod;

/**
 * auto create pojo
 * @author mycode
 * @version 1.0
 */
@JsonIgnoreProperties(ignoreUnknown=true)
public class Huser implements Saveable,Serializable{

	private static final long serialVersionUID = 1L;
	
	private static final String[] keyColumns = { "h_userid" };
	private static final String TABLENAME = "huser";

	@Override
	public String getTableName() {
		return TABLENAME;
	}

	@Override
	public String[] getKeyColumns() {
		return keyColumns;
	}
	private long h_userid = 0;
	
	@CacheKeyMethod
	public long geth_userid(){
		return h_userid;
	}
	
	public void seth_userid(long h_userid){
		this.h_userid = h_userid;
	}
	private String h_name = "";
	
	public String geth_name(){
		return h_name;
	}
	
	public void seth_name(String h_name){
		this.h_name = h_name;
	}
	private int h_age = 0;
	
	public int geth_age(){
		return h_age;
	}
	
	public void seth_age(int h_age){
		this.h_age = h_age;
	}
	
	@CacheKeyMethod
	public String getCacheKey(){
		return "huser_"+geth_userid();
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return h_name+":"+h_userid+":"+h_age;
	}
}



可以看到geth_userid 方法上声明了一个注释:@CacheKeyMethod,这个是用来标识memcached 进行缓存操作时获取key的方法。

我的Dao类中的相关方法如下:

@ReadThroughSingleCache(namespace="user")
	public Huser getHuserById(@ParameterValueKeyProvider long id) throws Exception{
		String sql = "select * from huser where h_userid=?";
		System.out.println("没有缓存命中");
		return (Huser) queryOne(sql,new Object[]{id}, new Huser());
	}

@UpdateSingleCache(namespace="user")
	public int modHuser(@ParameterValueKeyProvider @ParameterDataUpdateContent Huser entity) throws Exception{
		return modify(entity);
	}


@InvalidateSingleCache(namespace="user")
	public int delUser(@ParameterValueKeyProvider long key) throws Exception {
		String sql = "delete from huser where h_userid=?";
		return delete(sql,new Object[]{key});
	}



可以看到上面三个方法中有几个注释,现在分别对上面几个注释进行简单的介绍下:
@ReadThroughSingleCache
作用:读取Cache中数据,如果不存在,则将读取的数据存入Cache
key生成规则:ParameterValueKeyProvider指定的参数,如果该参数对象中包含CacheKeyMethod注解的方法,则调用其方法,否则调用toString方法

@InvalidateSingleCache
作用:失效Cache中的数据
key生成规则:
使用 ParameterValueKeyProvider注解时,与ReadThroughSingleCache一致

@UpdateSingleCache
作用:更新Cache中的数据
key生成规则:ParameterValueKeyProvider指定
ParameterDataUpdateContent:方法参数中的数据,作为更新缓存的数据

除了以上几种注释之外,SSM还提供了一些其它的注释,可以参孝下文
http://www.colorfuldays.org/program/java/ssm_memcache/

现在看看spring的配置文件:memcached-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context" 
        xsi:schemaLocation="http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

  <import resource="simplesm-context.xml" />
<context:property-placeholder location="classpath:memcached.properties" ignore-unresolvable="true"/> 
  <aop:aspectj-autoproxy />  

  <bean name="defaultMemcachedClient" class="com.google.code.ssm.CacheFactory">
    <property name="cacheClientFactory">
      <bean name="cacheClientFactory" class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" />
    </property>
    <property name="addressProvider">
      <bean class="com.google.code.ssm.config.DefaultAddressProvider">
      <!-- memcached 服务器地址,可配置多个,用“,”号隔开 -->
        <property name="address" value="${memcached.address}" />
      </bean>
    </property>
    <property name="configuration">
      <bean class="com.google.code.ssm.providers.CacheConfiguration">
      <!-- 是否使用哈希 -->
        <property name="consistentHashing" value="true" />
      </bean>
    </property>
    <!-- 该Memcached配置的Cache名称 一个应用中存在多个Memcached时,各个配置的cacheName必须不同。如果该值未设,系统默认为default 
    <property name="cacheName" value="mailbill"/>-->
  </bean>
</beans>



基本的配置及代码就写完了,现在写几个测试方法测试一下;MemcachedTest.java

package com.mailbill;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.mailbill.datacore.dao.HuserDao;
import com.mailbill.datacore.entity.Huser;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-jdbc-base.xml")
public class MemcachedTest extends AbstractJUnit4SpringContextTests {
	
	@Autowired
	private HuserDao dao;
	
	@Test
	public void save(){
		
		Huser user = new Huser();
		
		user.seth_name("hahah");
		user.seth_age(21);
		
		try {
			long userid = dao.addHuser(user);
			System.out.println(userid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	public void get(){
		try {
			Huser user  = dao.getHuserById(1);
			System.out.println(user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	public void update(){
		try {
			Huser user  = dao.getHuserById(2);
			
			user.seth_age(24);
			user.seth_name("xiaolongnv");
			
			dao.modHuser(user);
			
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@Test
	public void delete(){
		
		try {
			dao.delUser(2);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}



测试的时候可以使用MemcacheD Manager 工具进行监控memcached 的缓存状态,如图:

 

  • 大小: 63.4 KB
分享到:
评论
5 楼 CJaver 2014-10-24  
<script>alert("111");</script>
4 楼 CJaver 2014-10-24  
[list]
  • [list]
  • [*][list]
  • [*][*][list]
  • [*][*][*][list]
  • [*][*][*][*][list]
  • [*][*][*][*][*][list]
  • [*][*][*][*][*][*][list]
  • [*][*][*][*][*][*][*][list]
  • [*][*][*][*][*][*][*][*][list]
  • [*][*][*][*][*][*][*][*][*][list]
  • [*][*][*][*][*][*][*][*][*][*][list]
  • [*][*][*][*][*][*][*][*][*][*][*][list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][img][list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*]
    引用
    [list] [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*] [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][/list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][/list][/img]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][/list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][/list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][/list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][/list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][/list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][/list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][/list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][/list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][/list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][/list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][/list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][/list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][*][/list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][*][/list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][*][/list]
  • [*][*][*][*][*][*][*][*][*][*][*][*][/list]
  • [*][*][*][*][*][*][*][*][*][*][*][/list]
  • [*][*][*][*][*][*][*][*][*][*][/list]
  • [*][*][*][*][*][*][*][*][*][/list]
  • [*][*][*][*][*][*][*][*][/list]
  • [*][*][*][*][*][*][*][/list]
  • [*][*][*][*][*][*][/list]
  • [*][*][*][*][*][/list]
  • [*][*][*][*][/list]
  • [*][*][*][/list]
  • [*][*][/list]
  • [*][/list]
  • [/list]
  • [/list]
    3 楼 CJaver 2014-10-24  
    [/flash]
    ||||||||||||||
    |||||||||||||
    |||||||||||||
    ||||||||||||
    ||||||||||||
    |||||||||||
    |||||||||||
    ||||||||||
    ||||||||||
    |||||||||
    |||||||||
    ||||||||
    ||||||||
    |||||||
    |||||||
    ||||||
    ||||||
    |||||
    |||||
    ||||
    ||||
    |||
    |||
    ||
    ||
    |
    |
    2 楼 fxc13120 2014-02-10  
     <!-- 该Memcached配置的Cache名称 一个应用中存在多个Memcached时,各个配置的cacheName必须不同。如果该值未设,系统默认为default   
        <property name="cacheName" value="mailbill"/>-->  

    这一地方添加的cacheName,还得在Dao类上@CacheName("mailbill"),否则就会出错
    1 楼 yzhw 2013-11-18  
    分页查询能缓存吗?如果缓存了,更新某个记录后,分页查询能缓存会更新吗?

    相关推荐

      基于SSM框架,通过spring注解的方式,实现redis的数据缓存机制,将mysql的数据缓存到redis数据库.zip

      基于SSM框架,通过spring注解的方式,实现redis的数据缓存机制,将mysql的数据缓存到redis数据库.zip

      SSM 梳理 面试题整理

      该文档主要整理的是SSM的常见面试题,包括一下内容: 1. SpringMVC 的工作原理 (11步) 2. 谈谈你对SpringMVC的理解 3. SpringMVC 常用注解都有哪些? 4. Spring 的常用注解 5. 如何开启注解处理器和适配器? 6...

      使用SSM框架(SpringBoot、SpringSecurity、MyBatis)开发的一个简单校园多商家食堂点餐平台.zip

      2:成功整合了redis,并在项目中使用了redis的注解缓存功能,主要配置了商品类的缓存,并且当更新商品状态时清空缓存重新在数据库中查询后再存入缓存; 搭建了Linux环境,利用docker安装好了elasticsearch,同时在项目test...

      基于SSM的图书借阅系统

      基于SSM(Spring、Spring MVC 和 MyBatis)的图书借阅系统是一个典型的Java Web应用程序,它实现了图书馆管理系统的基本功能。该系统通过使用这些流行的开源框架和技术,可以轻松地构建可扩展、高性能且易于维护的Web...

      基于SSM的医院门诊挂号系统.zip

      本资源是一个基于SSM(Spring、Spring MVC、MyBatis)框架的医院门诊挂号系统。该系统旨在为医院提供一个高效、便捷的门诊挂号服务,帮助医院提高工作效率,优化患者就诊体验。技术选型:Spring:用于搭建应用程序上...

      最新SSM整合

      02、框架实现了零配置(通过注解实现),充分利用了struts框架、spring框架和myBatis框架提供的相关功能; 03、开发人员开发主要写6类文件:sql文、mapper、entity、service、action和jsp; (mapper是对应sql文的...

      ssm框架库存管理系统导入可用带sql

      我还负责了redis集成spring并缓存树 redis的使用主要涉及到两个注解的使用 1.Cache able:将方法的返回值在集合中查询出来之后缓存在redis中,之后的查询就不走数据库了 直接从redis中查询 2.CacheEvict:根据key...

      Springboot+Mybatis-plus+ SpringMvc+Shiro+Redis企业级报表后台管理系统

      报表后端采用技术: SpringBoot整合SSM(Spring+Mybatis-plus+ SpringMvc),spring security 全注解式的权限管理和JWT方式禁用Session,采用redis存储token及权限信息 报表前端采用Bootstrap框架,结合Jquery Ajax,...

      SSM与redis

      本项目针对初学spring、springmvc、mybatis、框架者,配置勒redis缓存,项目内都有详细的注解声明。!!!!!!!!!

      SSM:Spring+Springmvc+Mybatis框架,后续集成日志插件log4j、logback,集成Druid连接池监控,集成Redis缓存,office文件操作插件poi

      4.已经集成的Redis缓存,基于jedis使用缓存,实现客户端分片。 5.添加CommonInterceptor类实现HandlerInterceptorAdapter接口拦截请求,基于注解添加RedisAspect拦截类监视缓存方法调用。 6.集成office文件操作插件...

      SSM开发框架

      02、框架实现了零配置(通过注解实现),充分利用了struts框架、spring框架和myBatis框架提供的相关功能; 03、开发人员开发画面主要写6类文件:sql文、mapper、entity、service、action和jsp; (mapper是对应sql...

      Simple-Spring-Memcached

      Simple-Spring-Memcachd(SSM)企图通过实现几个基础的使用项来简化Memcached的使用。 该项在java-memcached客户端的基础上使用java5的注解和Sping/AspectJ的AOP,使能够在Sping中管理bean缓存。使用Simple-Spring-...

      SSM(详细注释代码清晰)开源框架

      02、框架实现了零配置(通过注解实现),充分利用了spring框架、springmvc框架和myBatis框架提供的相关功能; 03、开发人员开发画面主要写6类文件:controller、dao、mapping、pojo、service、serviceimp和jsp; ...

      Springboot+Mybatis-plus+ SpringMvc+Shiro+Redis企业级报表后台管理系统.rar

      shiro作为安全框架,主流技术 几乎零XML,极简配置 两套UI实现(bootstrap+layer ui),可以自由切换 报表后端采用技术: SpringBoot整合SSM(Spring+Mybatis-plus+ SpringMvc),spring security 全注解式的权限管理和...

      Crm客户关系管理系统

      技术实现: ●Spring+SpringMVC+Mybatis+JDK1.8+Maven+Quartz+Freemarker+...● 系统引入分布式缓存Redis技术,实现项目缓存统一处理,提高系统整体查询性能; 数据库和开发文档都全部打包,运行需要启动Redis等等。

      10分钟学会SpringBoot-与Redis缓存-5分钟掌握微服务代码生成器

      本课程全程使用目前比较流行的开发工具idea进行开发,采用现在互联网流行的微服务架构SpringBoot+SpringCloud+JPA, 同时也使用了互联网的高并发中间件redis,ElasticSearch,RabbitMQ,MongoDB数据库,springSecurity...

      吴天雄--shiro个人总结笔记.doc

      shiro标签,第六讲 自定义Realm(加入整合spring和MyBatis), 第七讲 加密(加密算法、加密过程、加密实现代码),第八讲 记住我,第九讲 缓存管理(代码实现),第十讲 会话管理(session的监听和检测、环境配置)...

      全新JAVAEE大神完美就业实战课程 超150G巨制课程轻松实战JAVAEE课程 就业部分.txt

      day05_CRM权限拦截器_SSH纯注解整合 day06_Easyui&列表展示 10-Oracle数据库(学习4天) Oracle_day01,安装_函数查询and条件查询 Oracle_day02,多表查询_子查询_集合运算 Oracle_day03,DDL,DML,视图,PLSQL编程 ...

    Global site tag (gtag.js) - Google Analytics