分享

spring memcache

 java_laq小馆 2013-09-02

学习了memcache,这是个好东西,分享一下自己的小实例,也方便以后查找使用

一、前期准备

1)  下载memcached服务端memcached-1.2.6-win32-bin.zip,地址:http://code.jellycan.com/memcached/

2)  下载java版客户端 java_memcached-release_2.6.1.zip
3)  解压缩memcached-1.2.6-win32-bin.zip到指定目录,例如:D:\memcached-1.2.6-win32 ,在终端(即cmd命令行界面)
 
D:\memcached-1.2.6-win32\memcached.exe -d install
D:\memcached\memcached.exe -d start
 
这样memcache就会作为windows系统服务在每次开机时启动memcache服务。
 
常用命令
 
-p 监听的端口 
-l 连接的IP地址, 默认是本机 
-d start 启动memcached服务 
-d restart 重起memcached服务 
-d stop|shutdown 关闭正在运行的memcached服务 
-d install 安装memcached服务 
-d uninstall 卸载memcached服务 
-u 以的身份运行 (仅在以root运行的时候有效) 
-m 最大内存使用,单位MB。默认64MB 
-M 内存耗尽时返回错误,而不是删除项 
-c 最大同时连接数,默认是1024 
-f 块大小增长因子,默认是1.25 
-n 最小分配空间,key+value+flags默认是48 
-h 显示帮助 



spring-memcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www./schema/beans"
    xmlns:xsi
="http://www./2001/XMLSchema-instance" xmlns:aop="http://www./schema/aop"
    xmlns:cache
="http://www./schema/cache"
    xmlns:context
="http://www./schema/context"
    xmlns:mvc
="http://www./schema/mvc" xmlns:oxm="http://www./schema/oxm"
    xmlns:p
="http://www./schema/p" xmlns:util="http://www./schema/util"
    xsi:schemaLocation
="http://www./schema/beans http://www./schema/beans/spring-beans.xsd  
    http://www./schema/aop http://www./schema/aop/spring-aop-3.1.xsd  
    http://www./schema/context http://www./schema/context/spring-context-3.1.xsd  
       http://www./schema/mvc http://www./schema/mvc/spring-mvc-3.1.xsd
       http://www./schema/util http://www./schema/util/spring-util-3.1.xsd">

    
<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>127.0.0.1:11211</value>
            
</list>
        
</property>
        
<property name="initConn">
            
<value>20</value>
        
</property>
        
<property name="minConn">
            
<value>10</value>
        
</property>
        
<property name="maxConn">
            
<value>50</value>
        
</property>
        
<property name="maintSleep">
            
<value>3000</value>
        
</property>
        
<property name="nagle">
            
<value>false</value>
        
</property>
        
<property name="socketTO">
            
<value>3000</value>
        
</property>
    
</bean>
    
    
<bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient">
        
<constructor-arg>
            
<value>neeaMemcachedPool</value>
        
</constructor-arg>
    
</bean>
    

</beans>






测试类:

package com.abin.lee.spring.memcache;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.danga.MemCached.MemCachedClient;

public class MemcacheUtilTest {
 static MemCachedClient memcachedClient;
 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
  ApplicationContext context= new ClassPathXmlApplicationContext("com/abin/lee/spring/memcache/spring-memcache.xml");
  memcachedClient= (MemCachedClient)context.getBean("memcachedClient");
 }

 @Test
 public void test() {
  memcachedClient.set("name", "abin");
  System.out.println(memcachedClient.get("name"));
 }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多