今天项目上要求使用Memcache对数据进行缓存操作,因为之前的项目中用过Memcache,所以整理下来。
第一步:引入jar;在pom文件中添加以下内容
<!-- memcache --> <dependency> <groupId>com.whalin</groupId> <artifactId>Memcached-Java-Client</artifactId> <version>3.0.0</version> </dependency>
第二步:配置Memcache相关属性(init.properties)
#######################设置Memcached服务器参数####################### #设置服务器地址 memcached.server=目标服务器ip:11211 #该端口号默认为11211 #容错 memcached.failOver=true #设置初始连接数 memcached.initConn=20 #设置最小连接数 memcached.minConn=10 #设置最大连接数 memcached.maxConn=250 #设置连接池维护线程的睡眠时间 memcached.maintSleep=3000 #设置是否使用Nagle算法(Socket的参数),如果是true在写数据时不缓冲,立即发送出去 memcached.nagle=false #设置socket的读取等待超时时间 memcached.socketTO=3000 #设置连接心跳监测开关 memcached.aliveCheck=true #######################设置Memcached服务器参数#######################
第三步:加载Memcache属性(memcached-content.xml)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd"> <!-- Memcached配置 --> <bean id="memcachedPool" class="com.whalin.MemCached.SockIOPool" factory-method="getInstance" init-method="initialize" destroy-method="shutDown"> <property name="servers"> <list> <value>${memcached.server}</value> </list> </property> <property name="initConn"> <value>${memcached.initConn}</value> </property> <property name="minConn"> <value>${memcached.minConn}</value> </property> <property name="maxConn"> <value>${memcached.maxConn}</value> </property> <property name="maintSleep"> <value>${memcached.maintSleep}</value> </property> <property name="nagle"> <value>${memcached.nagle}</value> </property> <property name="socketTO"> <value>${memcached.socketTO}</value> </property> </bean> </beans>
第四步:引入到spring配置文件中(applicationContext.xml)
<!-- 只需要加入这一句就可以 --> <import resource="memcached-content.xml"/>
ok,大功告成!至于Memcache应用, 可以网上搜以下,就是一个单纯的exe文件, 用的时候打开就行,如图, 打开后就是这个样子
要想看是否连接成功的话,可以在cmd中telnet 服务器地址 11211 (服务器地址与端口号之间是一个空格, 不要输入冒号)
相关推荐
在Spring MVC中配置Memcache,我们需要以下步骤: 1. **添加依赖**:首先,确保你的项目中包含了Memcache的客户端库,如spymemcached。在Maven的pom.xml文件中,添加如下依赖: ```xml <groupId>spy ...
标题 "spring memcache mybatis" 暗示了这是一个关于整合Spring、Memcached以及MyBatis的项目。这个项目可能是为了实现一个高效的数据缓存机制,利用Memcached的内存存储特性来提高应用性能,同时结合Spring框架的...
5. **缓存配置**:在Spring中配置Memcached,包括设置服务器地址、端口、超时时间、连接池大小等参数。还可以配置缓存的命名空间,以及序列化和反序列化的策略。 6. **缓存操作**:通过XMemcached的API,可以在应用...
SSM框架是Java Web开发中常用的整合框架,主要包括Spring、Spring MVC和MyBatis三个核心组件。本项目中,SSM框架与EXT、Ecache、Memcache、Quartz以及拦截器和Servlet等技术进行了深度整合,以实现更高效、功能更...
系统采用的技术主要有:Dubbo,memcache,mail,spring,fastjson,hibernate,druid,freemarker,jpa,spring data jpa,servlet3.1,shiro,fastjson,spring mvc,extjs4等 系统依赖的工具 Zookeeper,MemCache,Mysql(默认...
3. **配置Spring**:在Spring的配置文件中,配置Memcached的连接池和bean,例如: ```xml <bean id="memcachedClient" class="net.spy.memcached.spring.MemcachedClientFactoryBean"> ``` 这里,`...
其次,编写高质量的代码是必不可少的,需要对主流JAVA框架如Spring MVC、Spring Boot、Mybatis或iBatis、SSH等有熟练掌握,并能运用常用的设计模式。此外,对J2EE框架Spring的深入理解和灵活运用也是关键。同时,...
Spring MVC Spring WebFlow spring tx aop ioc Struts ibatis Mybatis CAS Dubbo 工作能力 软实力 应急能力 创新能力 管理能力 分享能力 学习能力 沟通能力 解决问题能力 经历 技术攻关案例 ...
其次,他熟悉常用项目管理工具如SVN、GIT、MAVEN和Jenkins,这些都是开发流程中不可或缺的工具。但是,熟悉这些工具并不一定能体现出在实际项目中的应用能力,例如自动化部署、持续集成/持续交付(CI/CD)的实践经验和...