Maven_Spring_Memcache 实例

Linux_Memcache 安装笔记

这里写图片描述

MemcachedUtil.java

package com.demo.memcache.util;

import java.util.Date;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.danga.MemCached.MemCachedClient;

public class MemcachedUtil {
    private static MemCachedClient client;

    static {
        client = new ClassPathXmlApplicationContext("applicationContext.xml").getBean("memCachedClient",
                MemCachedClient.class);
    }

    public static boolean keyExists(String key) {
        return client.keyExists(key);
    }

    public static boolean add(String key, Object value) {
        return client.add(key, value);
    }

    public static boolean add(String key, Object value, Date overtime) {
        return client.add(key, value, overtime);
    }

    public static boolean delete(String key) {
        return client.delete(key);
    }

    public static boolean flushAll() {
        return client.flushAll();
    }

    public boolean replace(String key, Object value) {
        return client.replace(key, value);
    }

    public boolean replace(String key, Object value, Date overtime) {
        return client.replace(key, value, overtime);
    }

    public static boolean set(String key, Object value) {
        return client.set(key, value);
    }

    public static boolean set(String key, Object value, Date overtime) {
        return client.set(key, value, overtime);
    }

    public static Object get(String key) {
        return client.get(key);
    }

    public static MemCachedClient getMemcachedClient() {
        return client;
    }

}

applicationContext.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <import resource="memcached.xml" />

</beans>

log4j.properties


log4j.rootLogger=info,console,DailyFile
log4j.additivity.org.apache=false

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%-5p] --> [%t] %l : %m %x %n

log4j.appender.DailyFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DailyFile.File=D:/data/logs/springjms.log
log4j.appender.DailyFile.DatePattern='.'yyyy-MM-dd
log4j.appender.DailyFile.layout=org.apache.log4j.PatternLayout
log4j.appender.DailyFile.layout.ConversionPattern=%-d{yyyy-MM-dd HH\:mm\:ss} [%-5p] --> [%t] %l \: %m %x %n

memcached.properties

#######################Memcached\u914d\u7f6e#######################
#\u670d\u52a1\u5668\u5730\u5740
memcached.server=192.168.216.140:11211
#\u521d\u59cb\u5316\u65f6\u5bf9\u6bcf\u4e2a\u670d\u52a1\u5668\u5efa\u7acb\u7684\u8fde\u63a5\u6570\u76ee
memcached.initConn=20
#\u6bcf\u4e2a\u670d\u52a1\u5668\u5efa\u7acb\u6700\u5c0f\u7684\u8fde\u63a5\u6570
memcached.minConn=10
#\u6bcf\u4e2a\u670d\u52a1\u5668\u5efa\u7acb\u6700\u5927\u7684\u8fde\u63a5\u6570
memcached.maxConn=50
#\u81ea\u67e5\u7ebf\u7a0b\u5468\u671f\u8fdb\u884c\u5de5\u4f5c\uff0c\u5176\u6bcf\u6b21\u4f11\u7720\u65f6\u95f4
memcached.maintSleep=3000
#Socket\u7684\u53c2\u6570\uff0c\u5982\u679c\u662ftrue\u5728\u5199\u6570\u636e\u65f6\u4e0d\u7f13\u51b2\uff0c\u7acb\u5373\u53d1\u9001\u51fa\u53bb
memcached.nagle=false
#Socket\u963b\u585e\u8bfb\u53d6\u6570\u636e\u7684\u8d85\u65f6\u65f6\u95f4
memcached.socketTO=3000

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:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-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/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/util 
           http://www.springframework.org/schema/util/spring-util-3.1.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

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

    <!-- Memcached配置 -->
    <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool"
        factory-method="getInstance" init-method="initialize" destroy-method="shutDown">
        <property name="servers">
            <list>
                <value>${memcached.server}</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>
        <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">
    </bean>

</beans>

MemcacheTest.java

package com.demo.memcache.test;

import org.junit.Test;

import com.demo.memcache.util.MemcachedUtil;

public class MemcacheTest {

    @Test
    public void memcache() {
        String key = "name";

        MemcachedUtil.add(key, "zhangsan");// 添加

        if (MemcachedUtil.keyExists(key)) {
            System.out.println(MemcachedUtil.get(key));
            MemcachedUtil.set(key, "lisi");// 更改
        }

        if (MemcachedUtil.keyExists(key)) {
            System.out.println(MemcachedUtil.get(key));
            MemcachedUtil.delete(key);//删除
        }

        if (!MemcachedUtil.keyExists(key)) {
            System.out.println(key + " deleted");
        }
    }

}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.demo</groupId>
    <artifactId>maven-spring-memcache</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!-- 本地自建maven仓库 -->
    <repositories>
        <repository>
            <id>nexus</id>
            <name>local private nexus</name>
            <url>http://10.18.0.100:8081/repository/maven-public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <dependencies>
        <!-- junit单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!-- spring整合Junit -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.0.RELEASE</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.12</version>
        </dependency>

        <!-- spring核心包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.memcache</groupId>
            <artifactId>java_memcached-release</artifactId>
            <version>2.6.6</version>
        </dependency>

        <dependency>  
            <groupId>commons-pool</groupId>  
            <artifactId>commons-pool</artifactId>  
            <version>1.6</version>  
        </dependency> 

    </dependencies>
    <build>
        <finalName>mk-remote</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

maven-spring-memcache 实例源码

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值