<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 41  文章 - 29  trackbacks - 0
    <2009年4月>
    2930311234
    567891011
    12131415161718
    19202122232425
    262728293012
    3456789

    常用鏈接

    留言簿(5)

    隨筆分類(lèi)(28)

    隨筆檔案(23)

    收藏夾(6)

    Inside JVM

    Java

    java performance

    Solr

    搜索

    •  

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    What is Cache?

    A “cache” is a mechanism used by computers to store frequently used information in a readily accessible place to reduce the need to retrieve that information repeatedly and needlessly.

    What is memcached?

    memcached is a memory cache; but it is so more than that. Memcache can be deployed anywhere and be accessed from anywhere over a network. Additionally the cache can span as many machines as you need, but no matter how many machines make up your memcached cluster, as far as your code is concerned memcached acts as a single cache so that you never have to worry about which machine your information is stored on. Finally, memcached is fast. It utilizes highly efficient, non-blocking networking libraries to ensure that memcached is always fast even under heavy load.

    What to cache?

    1. Query Results
    2. Objects with much calculation
    3. Thumbnails
    4. Anything that takes a while to generate

    How do I use memcached?

    1. Right before you query the database to retrieve data, check to see if that information is stored in the cache. If the data is found in memcached, then use the cached data as opposed to querying the database for the information.
    2. If the information is not found in memcached, then go ahead, call the database. Once you load the data you queried for, don’t forget to put in the cache. Then proceed normally. Now, in subsequent calls to fetch this information you don’t need to call the database at all.
    3. Now, if the information changes for some reason, a user updates the data for example, then delete the data from the cache. That way if someone tries to load it again, they will be forced to go back to the database to get it again. This keeps the cache fresh and accurate.

    Memcached Server - How does memcached work?

    Memcached's magic lies in its two-stage hash approach. It behaves as though it were a giant hash table, looking up key = value pairs. Give it a key, and set or get some arbitrary data.

    When doing a memcached lookup, first the client hashes the key against the whole list of servers. Once it has chosen a server, the client then sends its request, and the server does an internal hash key lookup for the actual item data.

    For example, if we have clients 1, 2, 3, and servers A, B, C:

    Client 1 wants to set key "foo" with value "barbaz". Client 1 takes the full list of servers (A, B, C), hashes the key against them, then lets say ends up picking server B. Client 1 then directly connects to server B, and sets key "foo" with value "barbaz". Next, client 2 wants to get key "foo". Client 2 runs the same client library as client 1, and has the same server list (A, B, C). It is able to use the same hashing process to figure out key "foo" is on server B. It then directly requests key "foo" and gets back "barbaz".

    Different client implementations store data into memcached differently (perl Storable, php serialize, java hibernate, JSON, etc). Some clients also implement the hashing algorithm differently. The server is always the same however.

    Finally, memcached itself is implemented as a non-blocking event-based server. This is an architecture used to solve the C10K problem and scale like crazy.

    The cache structure is an LRU (Least Recently Used), plus expiration timeouts. When you store items into memcached, you may state how long it should be valid in the cache. Which is forever, or some time in the future. If the server is out of memory, expired slabs are replaced first, then the oldest unused slabs go next.

    Memcahced Client - Java Examples

    Typically, you use a client library from your application to access one or more memcached servers. The Clients page has list of available API libraries, which are available for Perl, C, C#, PHP, Python, Java, Ruby, and Postgresql Stored Procedures and Triggers.

    To create cache client object and set params:

            MemCachedClient mc = new MemCachedClient();

            
    // compression is enabled by default    
            mc.setCompressEnable(true);

            
    // set compression threshhold to 4 KB (default: 15 KB)  
            mc.setCompressThreshold(4096);

            
    // turn on storing primitive types as a string representation
            
    // Should not do this in most cases.    
            mc.setPrimitiveAsString(true);

    To store an object:

            MemCachedClient mc = new MemCachedClient();
            String key   
    = "cacheKey1";     
            Object value 
    = SomeClass.getObject();   
            mc.set(key, value);

    To delete a cache entry:


            MemCachedClient mc = new MemCachedClient();
            String key   
    = "cacheKey1";     
            mc.delete(key);

    To retrieve an object from the cache:

            MemCachedClient mc = new MemCachedClient();
            String key   
    = "key";   
            Object value 
    = mc.get(key);

    To retrieve an multiple objects from the cache

            MemCachedClient mc = new MemCachedClient();
            String[] keys      
    = { "key""key1""key2" };
            Map
    <Object> values = mc.getMulti(keys);

    To flush all items in server(s) //Invalidates the entire cache.

            MemCachedClient mc = new MemCachedClient();
            mc.flushAll();

    To get stats from server(s) //Retrieves stats for all servers. Returns a map keyed on the servername. The value is another map which contains stats with stat name as key and value as value.

            MemCachedClient mc = new MemCachedClient();
            Map stats 
    = mc.stats();

    The Live Example From FaceBook

    200 dedicated memcached servers

    Each 16GB quad-core AMD 64

    3TB memcache


    Limits

    • Max Key Size

    A key uniquely identifies data stored in the cache, and is used when storing, retrieving and removing data from the cache. The maximum size of a key is 250 characters. Note this value will be less if you are using client "prefixes" or similar features, since the prefix is tacked onto the front of the original key. Shorter keys are generally better since they save memory and use less bandwidth.
    • Max Data Size

    The maximum size of a value you can store in memcached is 1 megabyte. If your data is larger, consider clientside compression or splitting the value up into multiple keys.




    posted on 2009-04-12 21:29 Justin Chen 閱讀(2100) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Web Architecture
    主站蜘蛛池模板: 午夜爱爱免费视频| 中国亚洲女人69内射少妇| 美女羞羞视频免费网站| 亚洲一区无码中文字幕 | 91精品全国免费观看含羞草| 亚洲国产综合精品| 亚洲福利精品电影在线观看| 亚洲国产精品免费视频| 亚洲日韩精品无码AV海量| 精品国产香蕉伊思人在线在线亚洲一区二区| 国产免费无码一区二区| 亚洲熟妇成人精品一区| 亚洲精品成人片在线播放| 最近最好的中文字幕2019免费| 国产97视频人人做人人爱免费| 亚洲人成在线精品| 亚洲国产婷婷综合在线精品 | 日韩精品无码人妻免费视频| 日本免费A级毛一片| 亚洲AV无码AV男人的天堂不卡 | 91情国产l精品国产亚洲区| 免费看无码自慰一区二区| 99久久国产精品免费一区二区| 亚洲第一成年网站视频| 亚洲伦理一区二区| 免费人妻av无码专区| 国产精品久久久久免费a∨| 国产一级a毛一级a看免费人娇| 亚洲最大av资源站无码av网址| 亚洲爆乳精品无码一区二区三区| 免费特级黄毛片在线成人观看| 日韩精品无码专区免费播放| xxxx日本在线播放免费不卡| 亚洲色成人四虎在线观看| 久久亚洲精品成人| 亚洲日韩精品无码专区网站| 小小影视日本动漫观看免费| 亚洲网站在线免费观看| 日韩精品在线免费观看| 久久er国产精品免费观看8| 羞羞视频网站免费入口|