Skip to content

Enyim Memcached 客户端几点偶遇

二进制协议

MemcachedClientConfiguration.Protocol = Binary,这是新版默认配置,但是如果你的Memcached版本不是在1.3以上,将无法支持二进制协议传输模式,特别是Memcached for win32 好像还是处于1.2.4版本上,这里只能需要配置 MemcachedClientConfiguration.Protocol = Text。

Get泛型

我是第一次使用这个版本的客户端,这里有个比较好的就是支持泛型的获取数据。基本实现方式:

return TryGet(key, out tmp) ? (T)tmp : default(T);

而Get

return this.TryGet(key, out tmp) ? tmp : null;

因此在本质上并没有太多的区别,减少类型转换,我建议使用as简单明了。

尽量只创建过少的MemcachedClient

Enyim.MemcachedClient支持不同池配置,我建议对每个Client做Singleton模式。

配置问题

其实我不想说这个事,但是我只是想提醒一下,Enyim.MemcachedClient在配置时,都是对套接字池的配置(即:Scoket Pool),对于入门来讲只要了解.NET中的套接字池配置就可以,哪怕不通过Web.Config,也可以通过很简单的MemcachedClientConfiguration.SocketPool套接字池配置,因些不要太过于复杂化。

Categories: 技术.

Tags:

Memcached for win32 命令描述

我的系统是WIN7,所以如果遇到什么Telnet无法使用的,是因为WIN7默认没有安装Telnet client。可能还有遇到权限问题,详见:windows7下安装Memcached

以下内容是转载的一篇文章,方便查阅:

Connecting to Memcached server with telnet

telnet <hostname> <port>

In the following \r\n mean you have to type enter in telnet console

If Memcached server send ERROR or CLIENT_ERROR, something is false with the command, if it’s SERVER_ERROR, server itself is in trouble

Retrieval Commands

Parameters :

<key> : the key of the data stored to retrieve

<key>* mean you can request value of multiple keys separated by whitespace

These commands return :

VALUE <flag> <bytes>\r\n<data> (see the following for explanation of <bytes> and <flag>)

END indicate the end of response

Get

Get value from key

Telnet command : get <key>*\r\n

get key
VALUE key 0 4
data
END

Gets

Get value from key along with a cas token (to use with cas command)

Telnet command : gets <key>*\r\n

get key
VALUE key 0 4
data
END

 

Storage Commands

Parameters :

<key> : the key of the data stored

<flags> : 32-bit unsigned integer that the server store with the data (provided by the user), and return along the data when the item is retrieved

<exptime> : expiration time in seconds, 0 mean no delay, if exptime is superior to 30 day, Memcached will use it as a UNIX timestamps for expiration

<bytes> : number of bytes in the data block

<cas unique> : unique 64-bit value of an existing entry (retrieved with gets command) to use with cas command

[noreply] : optional parameter that inform the server to not send the reply

These commands can return :

STORED to indicate success

NOT_STORED indicate that the data was not stored because condition for “add” or “replace” command wasn’t met, or the item is in a delete queue

EXISTS indicate that the item you are trying to store with “cas” command has been modified since last fetch

NOT_FOUND indicate that the item did not exist or has been deleted

Set

Store key/value pair in Memcached

Telnet command : set <key> <flags> <exptime> <bytes> [noreply]\r\n<value>\r\n

set key 0 900 4
data
STORED

Add

Store key/value pair in Memcached, but only if the server doesn’t already hold data for this key

Telnet command : add <key> <flags> <exptime> <bytes> [noreply]\r\n<value>\r\n

add key 0 900 2
10
STORED

Replace

Store key/value pair in Memcached, but only if the server already hold data for this key

Telnet command : replace <key> <flags> <exptime> <bytes> [noreply]\r\n<value>\r\n

replace key 0 900 2
10
STORED

Append

Add value to an existing key after existing data

Append does not take <flags> or <exptime> parameters but you must provide them !

Telnet command : append <key> <flags> <exptime> <bytes> [noreply]\r\n<value>\r\n

append key 0 900 2
10
STORED

Prepend

Add value to an existing key before existing data

Prepend does not take <flags> or <exptime> parameters but you must provide them !

Telnet command : prepend <key> <flags> <exptime> <bytes> [noreply]\r\n<value>\r\n

prepend key 0 900 2
10
STORED

Cas

Store data only if no one else has updated since the last fetch, determined with the cas tokem from gets command

Telnet command : cas <key> <flags> <exptime> <bytes> <cas unique> [noreply]\r\n

cas key 0 900 2 <cas unique>
10
STORED

 

Delete Command

Parameters :

<key> : the key to delete

[<time>] : optional time in second before real deletion of item

[noreply] : optional parameter that inform the server to not send the reply

These command can return :

DELETED to indicate success

NOT_FOUND indicate that the item did not exist or has been deleted

Delete

Store key/value pair in Memcached

Telnet command : delete <key> [<time>] [noreply]\r\n

delete key
DELETED

 

Increment/Decrement Commands

Parameters :

<key> : the key to delete

<value> : the amount to increase/decrease the item.

[noreply] : optional parameter that inform the server to not send the reply

These command can return :

NOT_FOUND indicate that the item did not exist or has been deleted

<value> increment and decrement return the result

Increment

Increment value associated with key in Memcached, item must exist, increment command will not create it

The limit of increment is the 64 bit mark

Telnet command : incr <key> <value> [noreply]\r\n

incr key 12
56

Decrement

Decrement value associated with key in Memcached, item must exist, decrement command will not create it

If you try to decrement a value bellow 0, value will stay at 0

Telnet command : decr <key> <value> [noreply]\r\n

decr key 12
44

 

Statistics Commands

Parameters :

<args> : argument to stats command

These command can return various stats that we will explain in the following

Basic stats

Return general-purpose statistics like uptime, version, memory occupation, …

Telnet command : stats\r\n

stats
STAT pid 106796
STAT uptime 5696
STAT time 1274397461
STAT version 1.4.5
STAT pointer_size 32
...
STAT total_items 344512124
STAT evictions 0
END

Items stats

Return items statistics, will display items statistics (count, age, eviction, …) organized by slabs ID

Telnet command : stats items\r\n

stats
STAT items:1:number 1125
STAT items:1:age 426
STAT items:1:evicted 0
STAT items:1:evicted_nonzero 0
STAT items:1:evicted_time 0
STAT items:1:outofmemory 0
STAT items:1:tailrepairs 0
...
STAT items:42:tailrepairs 0
END

Slabs stats

Return slabs statistics, will display slabs statistics (size, memory usage, commands count, …) organized by slabs ID

Telnet command : stats slabs\r\n

stats
STAT 1:chunk_size 80
STAT 1:chunks_per_page 13107
STAT 1:total_pages 1
STAT 1:total_chunks 13107
STAT 1:used_chunks 1125
...
STAT active_slabs 25
STAT total_malloced 10485600
END

 

Misc Commands

Flush_all

Flush the server key/value pair (invalidating them) after a optional [<time>] period

It always return OK

Telnet command : flush_all [<time>] [noreply]\r\n

flush_all 10
OK

Version

Return the Memcached server version

Telnet command : version\r\n

version
VERSION 1.4.5

Verbosity

Change the verbosity ouptut of Memcached server

It always return OK

Telnet command : verbosity <level> [noreply]\r\n

verbosity 2
OK

Quit

Server close connection when receiving this command

Telnet command : quit\r\n

quit

Categories: 技术.

Tags:

html5的设计

其实是在Jeremy Keith在 Fronteers 2010上的主题演讲,我只是做了一些归纳:
伯斯塔尔法则:发送时要保守;接收时要开放。

避免不必要的复杂性

1、简化doctype为:<!DOCTYPE html>
2、头部信息中发送字符编码简化为:<meta charset=”utf-8″>
3、CSS引用可以不需要type。
4、script可以不需要type。

支持已有的内容

1、支持大小写名称。
2、属性值可以不加引号。
3、标签可不闭合。
比如以下四段都是合法的:

<img src="foo" alt="bar" />
 <p>Hello world</p>
 <img src="foo" alt="bar">
 <p>Hello world
 <IMG SRC="foo" ALT="bar">
 <P>Hello world</P>
 <img src=foo alt=bar>
 <p>Hello world</p>

解决现实的问题

链接包含块级元素,这点也是我的最爱。

内容模型

新增四个内容模型:头部(header)、脚部(footer)、分区(section)、文章(article)。
而像footer未必要放在底下,最重要的是它们的语义,跟位置没有关系。
在HTML5中,只要你建立一个新的内容块,不管用section、article、aside、nav,还是别的元素,都可以在其中使用H1,而不必担心这个块里的标题在整个页面中应该排在什么级别;H2、H3,都没有问题。
html5的vedio

<video>
 <source src="movie.mp4">
 <source src="movie.ogv">
 <object data="movie.swf">
 <a href="movie.mp4">download</a>
 </object>
 </video>

上面的代码中包含了4个不同的层次。
1、如果浏览器支持video元素,也支持H264,没什么好说的,用第一个视频。
2、如果浏览器支持video元素,支持Ogg,那么用第二个视频。
3、如果浏览器不支持video元素,那么就要试试Flash影片了。
4、如果浏览器不支持video元素,也不支持Flash,我还给出了下载链接。

一旦遇到冲突,最终用户优先,其次是作者,其次是实现者,其次标准制定者,最后才是理论上的完满。

Categories: 技术.

Tags: , ,

谈男女贞操

原来是在一篇感人的文章中受启发:大致是讲一女孩子被几个人强奸,非常绝望想自我了断,可舍不得爱他的男人,于是用委婉形式告诉他了整个事实。当然完美的结局是男人提出在没结婚之前为他守住第二次贞操。

现代贞操观

贞操观在不同年代人的眼里是完全不同的。早在《西厢记》中有这样一段描写:张生与崔莺莺初试云雨后,看着落红斑斑的手帕,充满自得地唱道:”春罗元莹白,早见红香点嫩色。”张生因获得了莺莺的贞操,而自鸣得意、自我炫耀,而此时的崔莺莺也将贞洁之身看作是自己人生价值所在,她说:”妾千金之躯,一旦去之,此身皆托与足下,勿以它日见弃,使妾有白头之叹。”。我有这么一句某大型网站调查文字:

70后对待贞操有种“正室范儿”、80后女孩子会说:你是处男吗?你凭什么要求我是处女?、90后70%有异性朋友,其中90%发生性关系。

好了,不同年代看待贞操一直在变化,而近现代这种变化只能用“极速发展”来形容。那么贞操到底在现实生活中有什么作用呢?按我理解就简单两字:婚姻。

择偶策略

在贞操话题上男人和女人在择偶策略有很大不同。男人不可能完全“确定”自己到底是不是孩子真正父亲,而对于女人来讲不存在这个问题,因此对于女人来说并不需要关心或害怕我生的孩子不是自己。在动物世界里面除了河马等不主动向雌性求欢以达到繁衍下一代以外,所有雄性都会主动向雌性求欢。

很多雄性动物会在不同雌性身上播种,其主要原因是不想让自己基因从这个世上消失,当然这需要除了自然灾害等不可避免的因素,而恐龙就是一个活生生的例子。于是他们会寻求更多的雌性来播种,以便自己更多基因得以流传。由于只有雌性才是负责制造卵子个体的母体,所以对于雌性来说有更多的选择权。

对于以上这些,做为高等动物中的人类,更明白这个道理。

消失的基因

我前面讲过为了不让自己的基因消失而去到处播种,同时对于女性来讲更多选择权。看似两条理论都是对立的,但是从某种角度来讲又是相互存在利益关系。

首先男性不管怎么样,他们只有一个目的就是让自己的基因继续得以流传,而不会去管到底跟谁交配,毕竟生孩子不是男人有的,所以他们没有选择权。

其次对于女性也会有自己的考虑,她们更希望跟优秀基因交配,毕竟是从身上掉下来的肉。

 

Categories: 生活.

Tags:

windows7下安装Memcached

系统:Windows7

Memcached版本:memcached 1.2.4 Win32 Beta

在执行以下命令并没有得到成功的信息:

d:\memcached>memcached -d install

failed to install service or service already installed

d:\memcached>memcached -d start

failed to start service

我只是按常理“开始”-“运行”输入:cmd,结果你懂得……

解决办法就是以管理员身份运行CMD,怎么做呢?在开始-附件-右击“命令提示符”,以管理员身份运行,重新执行安装和启动指令就可以了。

Categories: 开源.

Tags:

获取中国天气www.weather.com.cn数据

现在有大部分网站都在使用中国天气网数据,以下我只是结合一些我的经验跟大家分享一下。以下有几点是需要注意的:

1、所有页面都是UTF-8格式编号。

2、项和项之间用英文逗号隔开,属性和属性之间有英文坚线隔开。

一、城市数据

中国天气网有自己的一整套从省、市、县地区,只提供到县地区数据。除了省级以外每个市、县地区都会有一个唯一编号,该编号用于获取天气数据的唯一凭证。另外要注意的是虽然地址上提供的是以XML为结尾的文件名,但是实际内容并非XML,因此对于获取的数据需要单独解析。

1、省级数据获取

省级数据直接从:http://www.weather.com.cn/data/listinfo/city.xml

01|北京,02|上海,03|天津,04|重庆,05|黑龙江,06|吉林,07|辽宁,08|内蒙古,09|河北,10|山西,11|陕西,12|山东,13|新疆,14|西藏,15|青海,16|甘肃,17|宁夏,18|河南,19|江苏,20|湖北,21|浙江,22|安徽,23|福建,24|江西,25|湖南,26|贵州,27|四川,28|广东,29|云南,30|广西,31|海南,32|香港,33|澳门,34|台湾

2、市、县级数据获取

市、县级(注:直辖市的市级为自身,县级为区域)的获取是按照前面省级ID从http://www.weather.com.cn/data/listinfo/city{省或市ID}.xml获得。

福建省下的市级数据:

2301|福州,2302|厦门,2303|宁德,2304|莆田,2305|泉州,2306|漳州,2307|龙岩,2308|三明,2309|南平

厦门市下的县级数据:

230201|厦门,230202|同安

比如厦门市的编号为230201并非是用于获取天气数据的编号,还需要再调用一次http://www.weather.com.cn/data/listinfo/city030201.xml得到的数据:

230201|101230201

其中101230201才是用于获取天气预报数据的唯一标识。

二、天气数据

中国天气网提供一整套JSON格式数据,获取地址http://m.weather.com.cn/data/{天气预报数据的唯一标识}.html。

{“weatherinfo”:{“city”:”北京”,”city_en”:”beijing”,”date_y”:”2011年5月8日”,”date”:”辛卯年”,”week”:”星期日”,”fchh”:”11″,”cityid”:”101010100″,”temp1″:”22℃~12℃”,”temp2″:”23℃~11℃”,”temp3″:”26℃~13℃”,”temp4″:”23℃~12℃”,”temp5″:”24℃~13℃”,”temp6″:”25℃~14℃”,”tempF1″:”71.6℉~53.6℉”,”tempF2″:”73.4℉~51.8℉”,”tempF3″:”78.8℉~55.4℉”,”tempF4″:”73.4℉~53.6℉”,”tempF5″:”75.2℉~55.4℉”,”tempF6″:”77℉~57.2℉”,”weather1″:”中雨转小雨”,”weather2″:”阴转晴”,”weather3″:”晴转阴”,”weather4″:”阵雨转晴”,”weather5″:”晴”,”weather6″:”晴”,”img1″:”8″,”img2″:”7″,”img3″:”2″,”img4″:”0″,”img5″:”0″,”img6″:”2″,”img7″:”3″,”img8″:”0″,”img9″:”0″,”img10″:”99″,”img11″:”0″,”img12″:”99″,”img_single”:”8″,”img_title1″:”中雨”,”img_title2″:”小雨”,”img_title3″:”阴”,”img_title4″:”晴”,”img_title5″:”晴”,”img_title6″:”阴”,”img_title7″:”阵雨”,”img_title8″:”晴”,”img_title9″:”晴”,”img_title10″:”晴”,”img_title11″:”晴”,”img_title12″:”晴”,”img_title_single”:”中雨”,”wind1″:”微风”,”wind2″:”微风”,”wind3″:”微风”,”wind4″:”微风转北风小于3级”,”wind5″:”北风3-4级转微风”,”wind6″:”微风”,”fx1″:”微风”,”fx2″:”微风”,”fl1″:”小于3级”,”fl2″:”小于3级”,”fl3″:”小于3级”,”fl4″:”小于3级转3-4级”,”fl5″:”3-4级转小于3级”,”fl6″:”小于3级”,”index”:”舒适”,”index_d”:”建议着薄型套装等春秋过渡装。年老体弱者宜着套装。但昼夜温差较大,注意适当增减衣服。”,”index48″:”舒适”,”index48_d”:”建议着薄型套装等春秋过渡装。年老体弱者宜着套装。但昼夜温差较大,注意适当增减衣服。”,”index_uv”:”最弱”,”index48_uv”:”最弱”,”index_xc”:”不宜”,”index_tr”:”一般”,”index_co”:”舒适”,”st1″:”18″,”st2″:”9″,”st3″:”24″,”st4″:”12″,”st5″:”26″,”st6″:”12″,”index_cl”:”不宜”,”index_ls”:”不宜”}}

1、变量名解释

temp{数字}:以date_y为基准未来六天天气情况,以摄氏度为准。

tempF{数字}:跟temp{数字}一样,只不过是以华氏度为准。

weather{数字}:天气现象。

img{数字}:天气现象的图片ID,这里可以从http://m.weather.com.cn/img/c{天气现象图片ID}.gif获取到一组图片。而每两组图片为一个图片标识分别是:白天和黑夜,因此才会有12组图片。

img_title{数字}:天气现象的图片title值。

wind{数字}:风向。

fx{数字}:风力。

最后是一些指数建议的数据。

2、数据更新频率

好像是每天的6、12、18时会更新一次,因此结合自身的情况同步更新。

整体的情况大概这样,希望整理这篇能给您带来方便。最后获取中国天气网数据是需要授权的,建议您在获取数据时先到http://www.weather.com.cn获取授权证明。

Categories: 技术.