尊重知識,轉(zhuǎn)載請注明本文來自:編程藝術(shù)家Poechant的CSDN博客 http://blog.csdn.net/potent
1、Cache定義
(1)狹義概念:用于CPU的相對高速處理與主存(Main Memory)的相對低速處理的之間起到協(xié)調(diào)功能的硬件設(shè)備。
(2)廣義概念:用于速度相差較大的兩種硬件之間,起到協(xié)調(diào)兩者數(shù)據(jù)傳輸速度差異的結(jié)構(gòu)。
狹義概念來自于Cache自1967出現(xiàn)以來較長時間內(nèi)的應(yīng)用場景。因?yàn)镃PU的數(shù)據(jù)處理速度,要遠(yuǎn)遠(yuǎn)高于主存,所以在CPU和主存之間會有高速緩存設(shè)備,甚至是多級緩存設(shè)備。而廣義概念,則是目前已經(jīng)被廣泛接受的一種定義,且廣義概念中,Cache不再只局限于硬件,也可以是軟件。比如用于網(wǎng)絡(luò)相對低速傳輸與磁盤相對高速傳輸之間的速度差異協(xié)調(diào)。
2、Cache的本質(zhì)原理
可以一句話概括,就是:Cache把要到慢速設(shè)備中取的數(shù)據(jù)預(yù)先放到快速設(shè)備中。
3、幾種類型的Cache
(1)CPU Cache:置于CPU和主存之間,用于加速CPU對主存的相對慢速操作。
(2)Browser Cache:置于客戶端與服務(wù)器之間,用于加速客戶端對服務(wù)器的相對慢速操作。
(3)Server Cache:置于網(wǎng)絡(luò)請求與本地文件之間,用于加速網(wǎng)絡(luò)請求對本地文件的相對慢速操作。
(4)CDN:CDN即Content Delivery Network,在各地設(shè)置的節(jié)點(diǎn)Cache,加速用戶對服務(wù)網(wǎng)絡(luò)的相對慢速操作。
(5)Database Cache
(6)OS Cache:內(nèi)存中存在的對于硬盤讀寫的緩沖區(qū)域。
4、What is Memcached?
Memcached是一個免費(fèi)開源、高性能、分布式的內(nèi)存對象緩存系統(tǒng)。Memcached是在內(nèi)存中,為特定數(shù)據(jù)(字符串或?qū)ο螅?gòu)建key-value的小塊數(shù)據(jù)存儲。
5、下載Memcached的服務(wù)器端軟件
Windows平臺版本下載:http://splinedancer.com/memcached-win32/memcached-1.2.4-Win32-Preview-20080309_bin.zip
Linux平臺版本下載:http://memcached.googlecode.com/files/memcached-1.4.10.tar.gz
6、在服務(wù)器上部署Memcached Server
以下以Windows平臺為例:
參考:http://www.codeforest.net/how-to-install-memcached-on-windows-machine
下載下來的Windows版本解壓到C:/memcached/
在控制臺輸入命令安裝:
- c:/memcached/memcached.exe -d install
啟動:
- c:/memcached/memcached.exe -d start
或:
- net start "memcached Server"
默認(rèn)的緩存大小為64M,如果不夠用,請打開注冊表,找到:
- HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/memcached Server .
將其內(nèi)容修改為:
- “C:/memcached/memcached.exe” -d runservice -m 512
7、下載Memcached的客戶端API包
下載地址:http://spymemcached.googlecode.com/files/memcached-2.5.jar
8、編寫一個Java數(shù)據(jù)類
- package com.sinosuperman.memcached;
-
- import java.io.Serializable;
-
-
- public class User implements Serializable{
-
- private static final long serialVersionUID = -372274003834027815L;
-
- String userId;
-
- public User(String userId) {
- super();
- this.userId = userId;
- }
-
- public String getUserId() {
- return userId;
- }
-
- public void setUserId(String userId) {
- this.userId = userId;
- }
-
- @Override
- public String toString() {
- // TODO Auto-generated method stub
- StringBuffer sb=new StringBuffer();
- sb.append("userId="+this.userId);
- return sb.toString();
- }
- }
9、編寫一個Memcached的客戶端
- package com.sinosuperman.memcached;
-
- import java.io.IOException;
- import java.net.InetSocketAddress;
-
- import net.spy.memcached.MemcachedClient;
-
- public class TestMemcached {
- public static void main(String[] args) throws IOException {
- MemcachedClient cache = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
- for (int i = 1; i < 10; i++) {
- cache.set("T0001" + i, 3600, new User(i + ""));
- }
- User myObject = (User) cache.get("T00011");
- System.out.println("Get object from mem :" + myObject);
- }
- }
10、運(yùn)行測試
運(yùn)行結(jié)果應(yīng)該如下:
- 2011-12-15 17:25:30.276 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
- 2011-12-15 17:25:30.292 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@c62080
- Get object from mem :userId=1
尊重知識,轉(zhuǎn)載請注明本文來自:編程藝術(shù)家Poechant的CSDN博客 http://blog.csdn.net/poechant-
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點(diǎn)擊舉報。