发表于: 2017-06-26 21:01:25

1 1239


今日完成

memcache

Memcache集群环境下缓存解决方案

Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。简单的说就是将数据调用到内存中,然后从内存中读取,从而大大提高读取速度。 

Memcache是danga的一个项目,最早是LiveJournal 服务的,最初为了加速 LiveJournal 访问速度而开发的,后来被很多大型的网站采用

其实Memcache是这个项目的名称,而memcached是它服务器端的主程序文件名

Memcached整合Spring

MemcachedCacheManager

package com.cdsmartlink.framework.cache.memcached;  

  

import java.util.Collection;  

import java.util.HashMap;  

import java.util.Map;  

import java.util.concurrent.ConcurrentHashMap;  

import java.util.concurrent.ConcurrentMap;  

  

import org.springframework.cache.Cache;  

import org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager;  

  

import com.danga.MemCached.MemCachedClient;  

  

/** 

 * Spring Cache整合Memcached实现  

 * @author liuyazhuang 

 */  

public class MemcachedCacheManager extends AbstractTransactionSupportingCacheManager {  

  

    private ConcurrentMap<String, Cache> cacheMap = new ConcurrentHashMap<String, Cache>();  

    private Map<String, Integer> expireMap = new HashMap<String, Integer>();   //缓存的时间  

    private MemCachedClient memcachedClient;   //memcached的客户端  

  

    public MemcachedCacheManager() {  

    }  

  

    @Override  

    protected Collection<? extends Cache> loadCaches() {  

        Collection<Cache> values = cacheMap.values();  

        return values;  

    }  

  

    @Override  

    public Cache getCache(String name) {  

        Cache cache = cacheMap.get(name);  

        if (cache == null) {  

            Integer expire = expireMap.get(name);  

            if (expire == null) {  

                expire = 0;  

                expireMap.put(name, expire);  

            }  

            cache = new MemcachedCache(name, expire.intValue(), memcachedClient);  

            cacheMap.put(name, cache);  

        }  

        return cache;  

    }  

  

    public void setMemcachedClient(MemCachedClient memcachedClient) {  

        this.memcachedClient = memcachedClient;  

    }  

  

    public void setConfigMap(Map<String, Integer> configMap) {  

        this.expireMap = configMap;  

    }  

  

}  



MemcachedCache

package com.cdsmartlink.framework.cache.memcached;  

  

import org.springframework.cache.Cache;  

import org.springframework.cache.support.SimpleValueWrapper;  

  

import com.danga.MemCached.MemCachedClient;  

  

/** 

 * MemcachedCache的实现,主要实现spring的cache接口 

 * @author liuyazhuang 

 * 

 */  

public class MemcachedCache implements Cache {  

  

    private final String name;  

    private final MemCache memCache;  

  

    public MemcachedCache(String name, int expire, MemCachedClient memcachedClient) {  

        this.name = name;  

        this.memCache = new MemCache(name, expire, memcachedClient);  

    }  

  

    @Override  

    public void clear() {  

        memCache.clear();  

    }  

  

    @Override  

    public void evict(Object key) {  

        memCache.delete(key.toString());  

    }  

  

    @Override  

    public ValueWrapper get(Object key) {  

        ValueWrapper wrapper = null;  

        Object value = memCache.get(key.toString());  

        if (value != null) {  

            wrapper = new SimpleValueWrapper(value);  

        }  

        return wrapper;  

    }  

  

    @Override  

    public String getName() {  

        return this.name;  

    }  

  

    @Override  

    public MemCache getNativeCache() {  

        return this.memCache;  

    }  

  

    @Override  

    public void put(Object key, Object value) {  

        memCache.put(key.toString(), value);  

    }  

  

    @Override  

    @SuppressWarnings("unchecked")  

    public <T> T get(Object key, Class<T> type) {  

        Object cacheValue = this.memCache.get(key.toString());  

        Object value = (cacheValue != null ? cacheValue : null);  

        if (type != null && !type.isInstance(value)) {  

            throw new IllegalStateException("Cached value is not of required type [" + type.getName() + "]: " + value);  

        }  

        return (T) value;  

    }  

  

}  

MemCache

package com.cdsmartlink.framework.cache.memcached;  

  

import java.util.HashSet;  

import java.util.Set;  

  

import org.slf4j.Logger;  

import org.slf4j.LoggerFactory;  

  

import com.danga.MemCached.MemCachedClient;  

  

/** 

 * Memcached的封装类 

 * @author liuyazhuang 

 * 

 */  

public class MemCache {  

    private static Logger log = LoggerFactory.getLogger(MemCache.class);  

  

    private Set<String> keySet = new HashSet<String>();  

    private final String name;  

    private final int expire;  

    private final MemCachedClient memcachedClient;  

  

    public MemCache(String name, int expire, MemCachedClient memcachedClient) {  

        this.name = name;  

        this.expire = expire;  

        this.memcachedClient = memcachedClient;  

    }  

  

    public Object get(String key) {  

        Object value = null;  

        try {  

            key = this.getKey(key);  

            value = memcachedClient.get(key);  

        } catch (Exception e) {  

            log.warn("获取 Memcached 缓存超时", e);  

        }  

        return value;  

    }  

  

    public void put(String key, Object value) {  

        if (value == null)  

            return;  

        try {  

            key = this.getKey(key);  

            memcachedClient.set(key, value, expire);  

            keySet.add(key);  

        }catch (Exception e) {  

            log.warn("更新 Memcached 缓存错误", e);  

        }  

    }  

  

    public void clear() {  

        for (String key : keySet) {  

            try {  

                memcachedClient.delete(this.getKey(key));  

            }catch (Exception e) {  

                log.warn("删除 Memcached 缓存错误", e);  

            }  

        }  

    }  

  

    public void delete(String key) {  

        try {  

            key = this.getKey(key);  

            memcachedClient.delete(key);  

        } catch (Exception e) {  

            log.warn("删除 Memcached 缓存被中断", e);  

        }  

    }  

  

    private String getKey(String key) {  

        return name + "_" + key;  

    }  

}  

applicationContext-memcached.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:p="http://www.springframework.org/schema/p"  

       xmlns:context="http://www.springframework.org/schema/context"  

       xmlns:aop="http://www.springframework.org/schema/aop"   

       xmlns:tx="http://www.springframework.org/schema/tx"  

       xmlns:mvc="http://www.springframework.org/schema/mvc"  

       xmlns:cache="http://www.springframework.org/schema/cache"  

       xsi:schemaLocation="http://www.springframework.org/schema/beans  

                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  

                        http://www.springframework.org/schema/context   

                        http://www.springframework.org/schema/context/spring-context-3.0.xsd  

                        http://www.springframework.org/schema/tx   

                        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  

                        http://www.springframework.org/schema/aop   

                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  

                        http://www.springframework.org/schema/mvc   

                        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  

                        http://www.springframework.org/schema/cache   

                        http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">  

  

      <!-- 扫描项目包的根路径 -->  

    <context:component-scan base-package="com.cdsmartlink" />  

    <context:component-scan base-package="com.cdsmartlink.utils.dao.base.impl"/>  

      

    <!-- ===================================  配置Memcached =============================== -->  

     <!-- 开启缓存 -->    

   <cache:annotation-driven cache-manager="cacheManager" proxy-target-class="true" />    

    <!-- 导入外部properties -->  

    <context:property-placeholder location="classpath:memcached.properties"/>  

      

     <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool"  

        factory-method="getInstance" init-method="initialize" destroy-method="shutDown">  

       <constructor-arg>  

            <value>neeaMemcachedPool</value>  

        </constructor-arg>  

        <property name="servers">  

            <list>  

                <value>${memcache.server}</value>  

            </list>  

        </property>  

        <property name="initConn">  

            <value>${memcache.initConn}</value>  

        </property>  

        <property name="minConn">  

            <value>${memcache.minConn}</value>  

        </property>  

        <property name="maxConn">  

            <value>${memcache.maxConn}</value>  

        </property>  

        <property name="maintSleep">  

            <value>${memcache.maintSleep}</value>  

        </property>  

        <property name="nagle">  

            <value>${memcache.nagle}</value>  

        </property>  

        <property name="socketTO">  

            <value>${memcache.socketTO}</value>  

        </property>  

    </bean>  

      

    <!-- 配置MemcachedClient -->  

    <bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient">  

        <constructor-arg>  

            <value>neeaMemcachedPool</value>  

        </constructor-arg>  

    </bean>  

      

    <!-- 配置缓存管理 -->  

    <bean id="cacheManager" class="com.cdsmartlink.framework.cache.memcached.MemcachedCacheManager">  

        <property name="memcachedClient" ref="memcachedClient"/>  

        <!-- 配置缓存时间 -->   

        <property name="configMap">  

             <map>  

                 <!-- key缓存对象名称   value缓存过期时间 -->   

                 <entry key="systemCache" value="3600"/>  

             </map>  

        </property>  

    </bean>  

      

    <!-- 导入调度任务 -->  

    <!-- <import resource="spring-quartz.xml" /> -->  

</beans>    

memcached.properties

memcache.server=192.168.254.120:12000  

memcache.initConn=20    

memcache.minConn=10    

memcache.maxConn=50    

memcache.maintSleep=3000    

memcache.nagle=false    

memcache.socketTO=3000   


web.xml

<?xml version="1.0" encoding="UTF-8"?>  

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   

        xmlns="http://java.sun.com/xml/ns/javaee"   

        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"   

        id="WebApp_ID" version="3.0">  

  <display-name>Smartlink</display-name>  

    

  <!-- 配置spring监听器 -->  

    <listener>  

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  

    </listener>  

      

     <!-- 配置初始化监听 -->  

    <listener>  

        <listener-class>com.cdsmartlink.utils.listener.WebServerStartListener</listener-class>  

    </listener>  

      

      

    <!-- 加载配置文件路径 -->  

    <context-param>  

        <param-name>contextConfigLocation</param-name>  

        <param-value>classpath*:applicationContext*.xml</param-value>  

    </context-param>  

      

    <!-- springmvc配置 -->  

    <servlet>  

        <servlet-name>smartlink</servlet-name>  

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  

        <load-on-startup>1</load-on-startup>  

    </servlet>  

    <servlet-mapping>  

        <servlet-name>smartlink</servlet-name>  

        <url-pattern>/</url-pattern>  

    </servlet-mapping>  

      

    <!-- 配置OpenSessionInView -->  

    <filter>  

        <filter-name>hibernateFilter</filter-name>  

        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>  

        <!-- singleSession默认为true,若设为false则等于没用OpenSessionInView -->  

        <init-param>  

            <param-name>singleSession</param-name>  

            <param-value>true</param-value>  

        </init-param>  

    </filter>  

      

    <filter-mapping>  

        <filter-name>hibernateFilter</filter-name>  

        <url-pattern>/*</url-pattern>  

     </filter-mapping>  

      

    <!-- 字符编码过滤器 -->  

    <filter>  

        <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>  

        <url-pattern>/*</url-pattern>  

    </filter-mapping>  

       

  <welcome-file-list>  

    <welcome-file>index.html</welcome-file>  

    <welcome-file>index.htm</welcome-file>  

    <welcome-file>index.jsp</welcome-file>  

    <welcome-file>default.html</welcome-file>  

    <welcome-file>default.htm</welcome-file>  

    <welcome-file>default.jsp</welcome-file>  

  </welcome-file-list>  

    

   <error-page>  

        <error-code>404</error-code>  

        <location>/static/html/page_404/404.html</location>  

    </error-page>  

</web-app>  



困难

收获

初步理解了memcache的概念,卡在整合Spring上

明日计划

memcache整合Spring


返回列表 返回列表
评论

    分享到