Memcache缓存读写
工作中经常用到缓存,写个工具页面来清理缓存:
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>My Web Application</display-name> <description> A application for test. </description> <servlet> <servlet-name>GetMemcache</servlet-name> <servlet-class>com.garinzhang.memcache.GetMemcache</servlet-class> </servlet> <servlet> <servlet-name>DeleteMemcache</servlet-name> <servlet-class>com.garinzhang.memcache.DeleteMemcache</servlet-class> </servlet> <servlet-mapping> <servlet-name>GetMemcache</servlet-name> <url-pattern>/getMemcache</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>DeleteMemcache</servlet-name> <url-pattern>/deleteMemcache</url-pattern> </servlet-mapping> </web-app>
index.jsp:
<?xml version="1.0" encoding="utf-8" ?> <%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %> <html> <head> <script type="text/javascript" src="./js/jquery-1.9.0.min.js"></script> <script type="text/javascript"> function submitForm(i) { var obj = {}; var postUrl = ""; if(i == '1') { postUrl = "./getMemcache"; } else { postUrl = "./deleteMemcache"; } //alert($("#cacheKeyId").val() + ":" + $("#ipPortId").val()); $.ajax({ type: "POST", url: postUrl, data: {cacheKey: $("#cacheKeyId").val(), ipPort: $("#ipPortId").val()} }).done(function(msg) { $("#cacheValue").val(msg); }); } </script> </head> <body> <div> <form action="#"> <label for="cacheKey">请输入IP&Port:</label> <input type="text" name="ipPort" id="ipPortId" value="192.168.0.1:12000" /> <label for="cacheKey">请输入缓存Key:</label> <input type="text" name="cacheKey" id="cacheKeyId" value="cache.key.word" size="40" /> <input type="button" name="getCache" value="查询缓存" onclick="submitForm(1);return false;" /> <input type="button" name="deleteCache" value="删除缓存" onclick="submitForm(2);return false;" /> </form> </div> <div style="width: 100%;"> <div style="float: left;"> <TEXTAREA rows="40" cols="80" readonly="readonly"> </TEXTAREA> </div> <div style="float: left;"> <TEXTAREA rows="40" cols="80" id="cacheValue" readonly="readonly"></TEXTAREA> </div> </div> </body> </html>
GetMemcache.java:
package com.garinzhang.memcache; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.rubyeye.xmemcached.MemcachedClient; import net.rubyeye.xmemcached.MemcachedClientBuilder; import net.rubyeye.xmemcached.XMemcachedClientBuilder; import net.rubyeye.xmemcached.utils.AddrUtil; public class GetMemcache extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); PrintWriter out = resp.getWriter(); String cacheKey = (String) req.getParameter("cacheKey"); String ipPort = req.getParameter("ipPort"); System.out.println("port is: " + ipPort + ", cacheKey: " + cacheKey); MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(ipPort)); MemcachedClient cachedClient = builder.build(); String output = ""; try { output = cachedClient.get(cacheKey.trim()); } catch(Exception e) { output = e.getMessage(); } out.print(output); System.out.println(output); } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
DeleteMemcache.java
package com.garinzhang.memcache; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.rubyeye.xmemcached.MemcachedClient; import net.rubyeye.xmemcached.MemcachedClientBuilder; import net.rubyeye.xmemcached.XMemcachedClientBuilder; import net.rubyeye.xmemcached.utils.AddrUtil; public class DeleteMemcache extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); PrintWriter out = resp.getWriter(); String cacheKey = req.getParameter("cacheKey").trim(); String ipPort = req.getParameter("ipPort").trim(); System.out.println("port is: " + ipPort + ", cacheKey: " + cacheKey); MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(ipPort)); MemcachedClient c = builder.build(); String output = ""; try { if(c.delete(cacheKey.trim())) { output = "Clear Success!"; } else { output = "Value already null!"; } } catch(Exception e) { output = e.getMessage(); } out.print(output); System.out.println(output); } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
用到的jar包:
commons-logging-1.1.jar,log4j-1.2.16.jar,slf4j-api-1.6.1.jar,xmemcached-1.3.5.jar,slf4j-nop-1.6.1.jar
方便打包,改成使用Maven进行配置,以下为POM文件内容:
<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>memcache_clear_online</groupId> <artifactId>memcache_clear_online</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <distributionManagement> <snapshotRepository> <id>nexus-snapshots</id> <name>nexus-snapshots</name> <url>http://192.168.200.203:9090/nexus/content/repositories/snapshots/</url> </snapshotRepository> <repository> <id>nexus-releases</id> <name>nexus-releases</name> <url>http://192.168.200.203:9090/nexus/content/repositories/releases/</url> </repository> </distributionManagement> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1</version> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> <exclusion> <groupId>logkit</groupId> <artifactId>logkit</artifactId> </exclusion> <exclusion> <groupId>avalon-framework</groupId> <artifactId>avalon-framework</artifactId> </exclusion> <exclusion> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>com.googlecode.xmemcached</groupId> <artifactId>xmemcached</artifactId> <version>1.4.1</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-nop</artifactId> <version>1.6.1</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/resources/misc</directory> </resource> <resource> <directory>src/main/resources/spring</directory> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> <testResources> <testResource> <directory>src/test/resources/misc</directory> </testResource> <testResource> <directory>src/test/resources/spring</directory> </testResource> <testResource> <directory>src/test/java</directory> <includes> <include>**/*.xml</include> </includes> </testResource> </testResources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
路慢慢其休远羲,吾将上下而求所