日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Memcached初探

發(fā)布時間:2025/3/20 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Memcached初探 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

初探的意思就是裝起來玩兒一把,最簡單的,所以這里寫的東西不一定對,小心!

1. install

2. simple client call

-------1--------

裝Memcached必須先裝Libevent。 下面是在mac上用brewhome裝的,簡單。相信自己手動裝的話也難度不大。

Libevent - brew install libevent -?/usr/local/Cellar/libevent/2.0.21_1

Memcached - brew install memcached - /usr/local/Cellar/memcached/1.4.20

根其他很多軟件如maven,ant之類的一樣,安裝好的目錄下面有bin目錄。進(jìn)去執(zhí)行如下命令運行即可。


Running Memcached:

Go to memcached install path/bin$?

sudo ./memcached -p 11212 -m 64 -u root -vv?

sudo?./memcached?-p?11211?-m?64?-u?root?-d

第二行意思是以關(guān)掉命令行也不會關(guān)掉Memecached。

命令的參數(shù)能猜個大概:

-p port

-m memory size(MB)

-u user

后面的自己查吧

--------2--------

memcached運行好在那邊之后,就在那邊孤獨的運行著了。 Memcached是和語言無關(guān)的,也就是說java,php什么的都可以調(diào)用它。所以,調(diào)用它的client端有很多實現(xiàn)。這里的簡單例子是用的google的xmemcached

<!--?xmemcached?-?https://code.google.com/p/xmemcached/?--><dependency><groupId>com.googlecode.xmemcached</groupId><artifactId>xmemcached</artifactId><version>2.0.0</version></dependency>
貼代碼這種方式不是太好,貼個小片段
public?void?testXmemcachedSimple()?throws?Exception?{MemcachedClient?client?=?new?XMemcachedClient("localhost",?11212);String?inputString?=?"Hello?Memcached!";//?store?a?value?for?one?hour(synchronously).client.set("key",?3600,?inputString);//?Retrieve?a?value.(synchronously).Object?someObject?=?client.get("key");//?Retrieve?a?value.(synchronously),operation?timeout?two?seconds.someObject?=?client.get("key",?2000);System.out.println("someObject?:?"?+?someObject);//?Touch?cache?item?,update?it's?expire?time?to?10?seconds.boolean?success?=?client.touch("key",?10);System.out.println("success?:?"?+?success);//?delete?valueclient.delete("key");}
這里只是最簡單的調(diào)用方式,可以有pool連接,同時連接多臺memcached服務(wù)器等。
google鏈接里所有的例子都有,很簡單,但是考慮到在墻內(nèi)登陸不方便,所以這里貼一點意思一下。
鏈接:https://code.google.com/p/xmemcached/

Memcached可以支持分布式部署,但是它們之間不是做數(shù)據(jù)同步的。而是對Client端來說是同一個而已。所以update持久化的數(shù)據(jù)必須同時update cacahe中的。

Spring 配置:

<bean?name="memcachedClient"class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean"?destroy-method="shutdown"><property?name="servers"><value>localhost:11212?192.168.234.81:11212</value></property><!--?server's?weights?--><property?name="weights"><list><value>1</value><value>2</value></list></property><!--?nio?connection?pool?size?--><property?name="connectionPoolSize"?value="3"></property><!--?Use?binary?protocol,default?is?TextCommandFactory?--><property?name="commandFactory"><bean?class="net.rubyeye.xmemcached.command.BinaryCommandFactory"></bean></property><!--?Distributed?strategy?--><property?name="sessionLocator"><bean?class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"></bean></property><!--?Serializing?transcoder?--><property?name="transcoder"><bean?class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder"?/></property><!--?ByteBuffer?allocator?--><property?name="bufferAllocator"><bean?class="net.rubyeye.xmemcached.buffer.SimpleBufferAllocator"></bean></property></bean>


更深層次的用法就不會了^_^











轉(zhuǎn)載于:https://my.oschina.net/joryqiao/blog/348669

總結(jié)

以上是生活随笔為你收集整理的Memcached初探的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。