memcached安装及与spring集成

一、在linux安装memcached,首先按照libevent包,再安装memcached,启动时如果找不到libevent,运行LD_DEBUG=libs /usr/local/memcached/bin/memcached -v,


然后创建软链接ln -s path topath,就可以了启动了。

二、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:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
	default-lazy-init="true">
	
	<context:property-placeholder location="classpath:application.properties"/>
	
	<bean id="memcachedPool" class="com.whalin.MemCached.SockIOPool"
		factory-method="getInstance" init-method="initialize" destroy-method="shutDown">
		<constructor-arg>
			<value>qwe</value>
		</constructor-arg>
		<property name="servers">
		<list>
			<value>${memcache.server}</value>
		</list>
		</property>
		<property name="initConn" value="${memcache.initConn}" />
		<property name="minConn" value="${memcache.minConn}" />
		<property name="maxConn" value="${memcache.maxConn}" />
		<property name="maxIdle" value="${memcache.maxIdle}" /> 
		<property name="maintSleep" value="${memcache.maintSleep}" />
		<property name="nagle" value="${memcache.nagle}" />
		<property name="socketTO" value="${memcache.socketTO}" />
		<property name="socketConnectTO" value="${memcache.socketConnectTO}"/>
	</bean>

	<bean id="cache" class="com.whalin.MemCached.MemCachedClient">
		<constructor-arg>
			<value>qwe</value>
		</constructor-arg>
	</bean>

</beans>
三、application.properties
#memcached configue
memcache.server=127.0.0.1:11211
memcache.initConn=20
memcache.minConn=10
memcache.maxConn=50
memcache.maxIdle=1800000
memcache.maintSleep=3000
memcache.nagle=false
memcache.socketTO=3000
memcache.socketConnectTO=3000


四、测试:

package com.csair.memcached;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.whalin.MemCached.MemCachedClient;
import com.whalin.MemCached.SockIOPool;

public class TestSpring {

	public static void main(String[] args) {
		String path = "applicationContext-memcached.xml";
		ApplicationContext context = new ClassPathXmlApplicationContext(path);
		SockIOPool pool = context.getBean("memcachedPool",SockIOPool.class);
		MemCachedClient cache = context.getBean("cache",MemCachedClient.class);
		cache.add("test", "ppt1");
		System.out.println(cache.get("test"));
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值