Memcached

Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载

Table of contents

安装

  1. 下载下来memcached.exe
  2. 切换到memcached.exe所在路径
  3. 输入memcached -d install
  4. win + r 输入 services.msc打开window服务
  5. 随便选中一个输入memcached就可以查看到安装好的服务,右击启动它,然后关闭窗口

使用

  1. 新建java工程或者maven工程

  2. 导入三个必备的依赖,fastjson-1.2.3,slf4j-api-1.7.5,xmemcached-2.3.2

  3. main方法中加入

     MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("127.0.0.1:11211"));
    builder.setSessionLocator(new KetamaMemcachedSessionLocator());
    try {
    MemcachedClient memcachedClient =builder.build(); //在这里写入测试代码 memcachedClient.shutdown();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (InterruptedException e) {
    e.printStackTrace();
    } catch (MemcachedException e) {
    e.printStackTrace();
    } catch (TimeoutException e) {
    e.printStackTrace();
    }

时效

	memcachedClient.set("testTime",2,"testTimeValue");
String testTime = memcachedClient.get("testTime");
System.out.println("testTime = " + testTime);
TimeUnit.SECONDS.sleep(4);
System.out.println("4s 过去了");
String valueExist = memcachedClient.get(testTime);
System.out.println("valueExist = " + valueExist);

输出:

testTime = testTimeValue

4s 过去了

valueExist = null

恢复

//存储,然后关闭掉服务
memcachedClient.set("test",0,"testValue");
String testTime=memcachedClient.get("test");
System.out.println("testTime="+testTime);
System.out.println("存储成功");

输出:

testTime = testValue

存储成功

//关闭掉服务后的重启
String testTime = memcachedClient.get("test");
System.out.println("testTime = " + testTime);
System.out.println("取值失败");

输出:

testTime = null

取值失败

delete

memcachedClient.set("test",0,"testValue");
String getVal = memcachedClient.get("test");
System.out.println("getVal = " + getVal);
memcachedClient.delete("test");
System.out.println("after delete ...");
getVal = memcachedClient.get("test");
System.out.println("getVal = " + getVal);

输出:

getVal = testValue

after delete ...

getVal = null

自动增长

//三个参数,第一个指定键,第二个指定递增的幅度大小,第三个指定当key不存在的情况下的初始值
for (int i = 0; i < 5; i++) {
memcachedClient.incr("博客的赞",1,20);
String point = memcachedClient.get("博客的赞");
System.out.println("point = " + point);
}

输出:

point = 20

point = 21

point = 22

point = 23

point = 24

关于incr的用法,值得警惕的是,它的值虽然看起来是一个数字,实际上正如代码中的String point = memcachedClient.get("博客的赞");

其实是一个字符串,所以会出现如下错误

memcachedClient.set("博客的赞1",0,10);
int str = memcachedClient.get("博客的赞1");
System.out.println("str1 = " + str);
memcachedClient.incr("博客的赞1",2,22);
str = memcachedClient.get("博客的赞1");
System.out.println("str2 = " + str);

输出:

net.rubyeye.xmemcached.exception.MemcachedClientException: cannot increment or decrement non-numeric value,key=博客的赞1

at net.rubyeye.xmemcached.command.Command.decodeError(Command.java:267)

..

at com.google.code.yanf4j.nio.impl.NioController.onRead(NioController.java:157)

at com.google.code.yanf4j.nio.impl.Reactor.dispatchEvent(Reactor.java:323)

at com.google.code.yanf4j.nio.impl.Reactor.run(Reactor.java:180)

str1 = 10

输出的顺序不同,注意输出的异常栈信息的第一条和后面的几条就指明nio.impl.Reactor.run,线程的,这儿就不深入展开了

spring

骨架

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-config.xml")
public class TestMem {
@Autowired
private MemcachedClient memcachedClient; @Test
public void test1(){
//测试代码部分
} }

测试代码

memcachedClient.set("springData",3,"dataVal");
String str = memcachedClient.get("springData");
System.out.println("str = " + str);

输出:

str = dataVal

还可以存储对象,不过该对象必须实现Serializable接口,不然会报错java.io.NotSerializableException: Teacher,

实现接口后

import lombok.Data;
import java.io.Serializable;
@Data
public class Teacher implements Serializable {
private int age;
private String name;
}

关于@Data关我在另一篇博客中有介绍lombok

Teacher teacher = new Teacher();
teacher.setAge(3);
teacher.setName("23"); memcachedClient.set("te", 0, teacher);
Teacher teacher1 = memcachedClient.get("te");
System.out.println("teacher1 = " + teacher1);

输出:

teacher1 = Teacher(age=3, name=23)

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.selton</groupId>
<artifactId>DemoMemSpring</artifactId>
<version>1.0</version> <dependencies> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
<scope>provided</scope>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.11.RELEASE</version>
</dependency> <dependency>
<groupId>com.googlecode.xmemcached</groupId>
<artifactId>xmemcached</artifactId>
<version>2.0.0</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.11.RELEASE</version>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency> </dependencies> </project>

config

<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
"> <bean id="memcachedClient" name="memcachedClient"
class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean">
<property name="servers">
<!--配置端口,另加入的话,空格隔开-->
<value>127.0.0.1:11211</value>
</property>
<property name="weights">
<list>
<!--设置不同端口的权重,这里只有一个端口-->
<value>1</value>
</list>
</property>
<property name="sessionLocator">
<bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"></bean>
</property>
<property name="transcoder">
<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
</property>
<property name="bufferAllocator">
<bean class="net.rubyeye.xmemcached.buffer.SimpleBufferAllocator"></bean>
</property>
</bean> </beans>

Memcached安装教程及使用的更多相关文章

  1. win7下64位系统memcache/memcached安装教程

    折腾了1个多小时,终于搞定.操作系统时64位的,php5.3.13 类似于上一篇的xdebug安装教程~~ memcache和memcached的区别  在自己的新程序中打算全面应用memcached ...

  2. wamp在win7下64位系统memcache/memcached安装教程

    折腾了1个多小时,终于搞定.操作系统时64位的,php5.3.13 类似于上一篇的xdebug安装教程~~ memcache和memcached的区别  在自己的新程序中打算全面应用memcached ...

  3. memcache/memcached安装教程并应用Tinkphp3.2

    在自己的新程序中打算全面应用memcached技术,这个很容易理解这是memcached是内存缓存,但是怎么还有memcache呢?其实很简单,memcache是php的一个扩展,用于php管理mem ...

  4. Linux下memcached安装与连接

    前几天技术总监要我在项目中加一个memcached,以前也从来没有配置过,所以就去网上找教程,最终折腾成功.比较坑的就是sasl协议那里. 由于memcached依赖libevents,所以要下载两个 ...

  5. 淘宝Tengine 2.1.2 稳定版(nginx/1.6.2) Centos 6.5安装教程

    淘宝Tengine 2.1.2 稳定版(nginx/1.6.2) Centos 6.5 安装教程 Tengine 简介: Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大 ...

  6. Redis笔记(一):Redis安装教程

    Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. Redis是目前应用最广泛的内存数据存储技术,相比之前的Me ...

  7. memcached 安装使用

    一.Memcached和Memcache的区别: 网上关于Memcached和Memcache的区别的理解众说纷纭,我个人的理解是: Memcached是一个内存缓存系统,而Memcache是php的 ...

  8. php 安装教程

    php 安装教程 本文采用php7.0.1作为样例,进行安装. 系统环境: CentOS6.7. gcc 4.8.2 libzip 1.0.1 在安装之前,可以先更新CentOS系统. yum -y ...

  9. Linux+apache+mono+asp.net安装教程

    Linux+apache+mono+asp.net安装教程(CentOS上测试的) 一.准备工作: 1.安装linux系统(CentOS,这个就不多讲了) 2.下载所需软件 http-2.4.4.ta ...

随机推荐

  1. uva10288 Coupons 【概率 分数】

    题目: 题意: 一共n种不同的礼券,每次得到每种礼券的概率相同.求期望多少次可以得到所有n种礼券.结果以带分数形式输出.1<= n <=33. 思路: 假设当前已经得到k种,获得新的一种的 ...

  2. Linux基石【第四篇】基本Linux命令

    Linux 系统上一切皆文件 命令: pwd  -- 查看当前目录  / 代表根目录 clear -- 清屏命令 cd(change directory) -- 切换目录 cd / -- 切换到根目录 ...

  3. c语言字符串指针

    最近正在看c语言,在指针这块遇到了麻烦,特别是字符串指针这块,简单记录下. 字符串指针 void main() { char *p = "tasklist"; printf(&qu ...

  4. Shiro和Spring 集合实现同一个账号只能一个人在线使用,其它人在使用进行剔除(八)

    1.实现原理其实就是自定义过滤器,然后登录时,A登录系统后,B也登录了,这个时候获取此账号之前的session给删除,然后将新的session放入到缓存里面去,一个账户对应一个有序的集合 编写自定义过 ...

  5. 关于Yii2.0的url路径优化问题(配置虚拟路径)

    backend/config/main.php 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => fa ...

  6. 关于int转char类型引发的一些思考

    signed char unsigned char

  7. mongo学习- 副本集 大多数原则

    副本集中有一个重要的概念“大多数”,意思是说,选择主节点需要大多数决定(本人亲自做了实验) 步骤: 1.开启副本集(如果没有配置好 副本集的 亲参考我的上篇文章  https://www.cnblog ...

  8. word 2007 写CSDN博客

    目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...

  9. Java Annotation Processors

    Table Of Contents 1. Introduction 2. When to Use Annotation Processors 3. Annotation Processing Unde ...

  10. 在Ubuntu14.04上安装WordPress4搭建技术博客

    1.安装LAMP环境 1.1 安装Apache2 1.2 安装MySQL5 1.3 安装PHP5 1.4 安装phpMyAdmin 2.初始化数据库 3.下载并配置WordPress 4.配置Apac ...