Memcached安装教程及使用
Memcached
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载
Table of contents
安装
- 下载下来memcached.exe
- 切换到memcached.exe所在路径
- 输入memcached -d install
- win + r 输入 services.msc打开window服务
- 随便选中一个输入memcached就可以查看到安装好的服务,右击启动它,然后关闭窗口
使用
新建java工程或者maven工程
导入三个必备的依赖,
fastjson-1.2.3,slf4j-api-1.7.5,xmemcached-2.3.2main方法中加入
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
- 新建一个maven工程
- pom.xml
- 在resource中新建sping-config.xml
- 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安装教程及使用的更多相关文章
- win7下64位系统memcache/memcached安装教程
折腾了1个多小时,终于搞定.操作系统时64位的,php5.3.13 类似于上一篇的xdebug安装教程~~ memcache和memcached的区别 在自己的新程序中打算全面应用memcached ...
- wamp在win7下64位系统memcache/memcached安装教程
折腾了1个多小时,终于搞定.操作系统时64位的,php5.3.13 类似于上一篇的xdebug安装教程~~ memcache和memcached的区别 在自己的新程序中打算全面应用memcached ...
- memcache/memcached安装教程并应用Tinkphp3.2
在自己的新程序中打算全面应用memcached技术,这个很容易理解这是memcached是内存缓存,但是怎么还有memcache呢?其实很简单,memcache是php的一个扩展,用于php管理mem ...
- Linux下memcached安装与连接
前几天技术总监要我在项目中加一个memcached,以前也从来没有配置过,所以就去网上找教程,最终折腾成功.比较坑的就是sasl协议那里. 由于memcached依赖libevents,所以要下载两个 ...
- 淘宝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的基础上,针对大 ...
- Redis笔记(一):Redis安装教程
Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. Redis是目前应用最广泛的内存数据存储技术,相比之前的Me ...
- memcached 安装使用
一.Memcached和Memcache的区别: 网上关于Memcached和Memcache的区别的理解众说纷纭,我个人的理解是: Memcached是一个内存缓存系统,而Memcache是php的 ...
- php 安装教程
php 安装教程 本文采用php7.0.1作为样例,进行安装. 系统环境: CentOS6.7. gcc 4.8.2 libzip 1.0.1 在安装之前,可以先更新CentOS系统. yum -y ...
- Linux+apache+mono+asp.net安装教程
Linux+apache+mono+asp.net安装教程(CentOS上测试的) 一.准备工作: 1.安装linux系统(CentOS,这个就不多讲了) 2.下载所需软件 http-2.4.4.ta ...
随机推荐
- 深入理解Spring的容器内事件发布监听机制
目录 1. 什么是事件监听机制 2. JDK中对事件监听机制的支持 2.1 基于JDK实现对任务执行结果的监听 3.Spring容器对事件监听机制的支持 3.1 基于Spring实现对任务执行结果的监 ...
- UNITY 多SCENE加载与编辑
Unity内部场景的加载分为两步: Loading.是指从文件.内存(主要是Streamed scene AssetBundle)中加载Scene的内容,创建并读取所有相关的Game objects. ...
- 在Action中获取表单提交数据
-----------------siwuxie095 在 Action 中获取表单提交数据 1.之前的 Web 阶段是提交表单到 Servlet,在其中使用 Request 对象 的方法获取数据 2 ...
- WebBrowser介绍——Javascript与C++互操作
WebBrowser控件是Microsoft提供的一个用于网页浏览的客户端控件,WebBrowser控件的使用相当广泛,例如很多邮件客户端都是使用可编辑的WebBrowser控件作为写邮件的工具,也有 ...
- code1001 舒适的路线
n次最小生成树kruskal 将所有的边排序,权值小的在前. 设排序后第i条边为路径中的最长边,那么这条路径一定是由1~i中的一些边组成 因为最高速和最低速的差尽量小,最高速确定了,最低速应尽量大. ...
- FW:程序在内存的划分(转)
一.预备知识—程序的内存分配 一个由c/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中的栈. ...
- 我的border能自定义四角之border-radius : 左上角,右上角,左下角,右下角。
1 边框:border: 1px solid #0081df; 2 想要单独加上四个圆角: border-bottom-left-radius: 5px; border-top-left-radius ...
- 整理Javascript基础数据和引用数据复制值的问题
Javascript数据分为两大类:1.基础类型(原始类型数据) 2.引用类型.他们的存储方式是不同的 基础类型的数据存储是保存在栈内存中的: 例如: var a=1; var b=a; var a= ...
- UISearchDisplayController
// // FirstViewController.swift // SearchDisplayDemo // // Created by Bruce Lee on 24/12/14. // Copy ...
- Swift使用CoreLocation,你必须要看这一篇
CoreLocation,在我们这里讨论的是一个最常用的东西,就是用Location Manger获取用户当前的位置. 整个的来说非常简单.只要这样: import CoreLocation 需要使用 ...