Memcached安裝教程及使用
Memcached
Memcached 是一個高性能的分布式內存對象緩存系統,用於動態Web應用以減輕數據庫負載
Table of contents
- 安裝
- 使用
- 在spring中使用
安裝
- 下載下來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.2
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(); }
- 測試一定時間後數據是否還能取出來
- 永久時間,測試重啟服務之後先前存儲的數據是否還能取出來
- delete
- 自動增長
時效
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安裝教程及使用