发表于: 2020-06-29 21:41:15

1 1809


今天完成的事情:Memcache

什么是Memcache

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

为什么会有Memcache和memcached两种名称

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

在电脑上配置Memcache

管理员运行以下命令

schtasks /create /sc onstart /tn memcached /tr "E:\java\memcached\memcached-amd64\memcached.exe' -m 512 "

Spring集成Memcached 

(1)Memcached Client for Java 比 SpyMemcached更稳定、更早、更广泛; 
(2)SpyMemcached d 比 Memcached Client for Java更高效; 
(3)XMemcached 比 SpyMemcache并发效果更好。

选用第三种 编写工具类

依赖

 <!-- xmemcached-->
 <dependency>
   <groupId>com.googlecode.xmemcached</groupId>
   <artifactId>xmemcached</artifactId>
   <version>2.4.4</version>
 </dependency>
</dependencies>

xmemcached.properties配置文件:

#连接池大小即客户端个数
memcached.connectionPoolSize=1
memcached.failureMode=true
#server1
memcached.server1.host=127.0.0.1
memcached.server1.port=11211
memcached.server1.weight=1

xmemcached.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:mvc="http://www.springframework.org/schema/mvc"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
     http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
   <!-- properties config   -->
   <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="order" value="1"/>
       <property name="ignoreUnresolvablePlaceholders" value="true"/>
       <property name="locations">
           <list>
               <value>classpath:properties/xmemcached.properties</value>
           </list>
       </property>
   </bean>
   <!-- Memcached配置 -->
   <!-- p:connectionPoolSize="${memcached.connectionPoolSize}"   p:failureMode="${memcached.failureMode}" -->
   <bean   id="memcachedClientBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder">
       <constructor-arg>
           <list>
               <bean class="java.net.InetSocketAddress">
                   <constructor-arg value="${memcached.server1.host}"/>
                   <constructor-arg value="${memcached.server1.port}"/>
               </bean>
           </list>
       </constructor-arg>
       <constructor-arg>
           <list>
               <value>${memcached.server1.weight}</value>
           </list>
       </constructor-arg>
       <property name="commandFactory" >
           <bean class="net.rubyeye.xmemcached.command.TextCommandFactory" />
       </property>
       <property name="sessionLocator" >
           <bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator" />
       </property>
       <property name="transcoder" >
           <bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
       </property>
       <property name="connectionPoolSize" value="${memcached.connectionPoolSize}" />
       <property name="failureMode" value="${memcached.failureMode}" />
   </bean>
   <!-- Use factory bean to build memcached client -->
   <bean id="memcachedClient" factory-bean="memcachedClientBuilder" factory-method="build"  destroy-method="shutdown" />
</beans>

memcached工具类

package util;

import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.TimeoutException;
public class XmemcachedManager {
private static final MemcachedClient memcachedClient;

   static {
ApplicationContext context = new ClassPathXmlApplicationContext("xmemcached.xml");
       memcachedClient = (MemcachedClient) context.getBean("memcachedClient");
   }
private static final int EXP = 60*60; //seconds
   private static final long TIME_OUT = 60*1000; //millis

   /**
    * 设置缓存数据
    * @param key key
    * @param value value
    */
   public void set(String key,String value) throws InterruptedException, MemcachedException, TimeoutException {
memcachedClient.set(key, EXP,value);
   }

/**
    *替换缓存数据
    */
   public void replace(String key,String value) throws InterruptedException, MemcachedException, TimeoutException {
memcachedClient.replace(key, EXP,value);
   }

/**
    * 删除缓存数据
    */
   public void delete(String key) throws InterruptedException, MemcachedException, TimeoutException {
memcachedClient.delete(key);
   }

/**
    * 获取缓存数据
    * @param key
    * @return Object
    */
   public Object get(String key) throws InterruptedException, MemcachedException, TimeoutException {
return memcachedClient.get(key);
   }
public Map<String,Object> get(Collection<String> keys) throws InterruptedException, MemcachedException, TimeoutException {
return memcachedClient.get(keys);
   }
}


明天计划的事情:任务六






返回列表 返回列表
评论

    分享到