由于项目需要简单的理解了一些memcached的运行,现在比较流行的缓存框架是Redis,本文不介绍了。
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。
memcached作为高速运行的分布式缓存服务器,具有以下的特点。· 协议简单· 基于libevent的事件处理· 内置内存存储方式· memcached不互相通信的分布式不多说理论了,直接上干货第一种方式不通过sPRingMyMemcachedUtil工具类测试第二种方式通过spring集成配置applicationContext.xml<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="order" value="1"/> <property name="ignoreUnresolvablePlaceholders" value="true"/> <property name="locations"> <list> <!--<value>classpath:/com/springmvc/config/memcached.properties</value>--> <value>/WEB-INF/config/memcached.properties</value> </list> </property> </bean> <!-- 客户端:java_memcached-release_2.6.3 --> <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance" init-method="initialize" lazy-init="false" destroy-method="shutDown"> <constructor-arg> <value>memcachedPool</value> </constructor-arg> <!-- 可以设置多个memcached服务器 --> <property name="servers"> <list> <value>${memcached.server1}:${memcached.port1}</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> <!-- TCP/Socket的参数,如果是true在写数据时不缓冲,立即发送出去参数 --> <property name="nagle"> <value>${memcached.nagle}</value> </property> <!-- 连接超时/阻塞读取数据的超时间是 --> <property name="socketTO"> <value>${memcached.socketTO}</value> </property> </bean> <bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient" > <constructor-arg> <value>memcachedPool</value> </constructor-arg> </bean>配置memcached.properties文件#服务器地址 memcached.server1=127.0.0.1memcached.port1=11211#memcached.server=127.0.0.1:11211#初始化时对每个服务器建立的连接数目 memcached.initConn=20 #每个服务器建立最小的连接数 memcached.minConn=10 #每个服务器建立最大的连接数 memcached.maxConn=50 #自查线程周期进行工作,其每次休眠时间 memcached.maintSleep=3000 #Socket的参数,如果是true在写数据时不缓冲,立即发送出去 memcached.nagle=false #Socket阻塞读取数据的超时时间 memcached.socketTO=3000配置spring-servlet-config.xml<!-- 使用@Controllers前配置 --> <mvc:annotation-driven /> <!-- 容器加载时 自动扫描所有注解 --> <context:component-scan base-package="com.test" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /> <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" /> <context:include-filter type="annotation" expression="org.springframework.stereotype.Component" /> </context:component-scan> <!-- 配置静态资源 --> <mvc:resources mapping="/js/**" location="/js/" /> <mvc:resources mapping="/image/**" location="/image/" /> <mvc:resources mapping="/CSS/**" location="/css/" /> <!-- 使用jsp作为视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass"> <value>org.springframework.web.servlet.view.JstlView</value> </property> <!-- 目标路径返回到pages下 使用jsp作为视图 --> <property name="prefix" value="/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 异常处理 --> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="org.apache.shiro.authz.UnauthorizedException">error/403</prop> </props> </property> </bean>配置web.xml<!-- 引入 spring --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/applicationContext..xml</param-value> </context-param> <!-- 引入 springMVC --> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring-servlet-config.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 编码 UTF-8 --> <filter> <filter-name>SpringMVC-Memcached-Encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>SpringMVC-Memcached-Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>测试类contoller@RequestMapping("/login") public ModelAndView login( @RequestParam(value = "username") String username, @RequestParam(value = "passWord") String password, Httpsession session){ ModelAndView m = new ModelAndView(); m.setViewName("index");//成功返回/pages/index.jsp Memcached.set("wasd", "12ab",new Date(1000 * 60)); Object ss = Memcached.get("wasd"); System.out.println(ss.toString()); m.addObject("returnMsg","好的!"); return m; }效果图相关jar
新闻热点
疑难解答