Showing posts with label memcached. Show all posts
Showing posts with label memcached. Show all posts

Tuesday, January 10, 2012

Memcached Java Client Simple Usage

1. Download library from here.
2. Import java_memcached-release_2.6.3.jar into your project.
3. Simple code for testing:

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
public class MemCacheClient {
static{
String[] serverList = {"localhost:11211"};
Integer[] weight = {3};
SockIOPool pool = SockIOPool.getInstance();
pool.setServers(serverList); 
pool.setWeights(weight);
pool.setInitConn(5);
pool.setMinConn(5);
pool.setMaxConn(250);
pool.setMaxIdle(1000*60*60*6);
pool.initialize();
}
public static void main (String args[]){
MemCachedClient mcc = new MemCachedClient();
mcc.set("testA", "AA");
mcc.set("testB", "BB");
System.out.println(mcc.get("testA"));
System.out.println(mcc.get("testB"));
mcc.delete("testA");
System.out.println(mcc.get("testA"));
}
}


Sunday, January 8, 2012

Memcached Server Installation

For Windows
1. download memcached for windows (memcached-1.2.6-win32-bin.zip) from here
2. unzip memcached-1.2.6-win32-bin.zip in any folder (e.g., D:\memcached-1.2.6-win32\).
3. install: D:\memcached-1.2.6-win32\memcached.exe -d install.
4. start: memcached.exe -d start.



Yum install
1. using yum to install memcached
# yum install memcached
2. 開機啟動 memcached 設定
# chkconfig memcached on
3. 啟動 memcached server 
# service memcached start
4. 簡單測試 memcached service
# echo stats | nc localhost 11211
會產生以下內容: 
STAT pid 13576
STAT uptime 1114
STAT time 1326179848
STAT version 1.4.10
STAT libevent 1.4.13-stable
STAT pointer_size 64
STAT rusage_user 0.008998
STAT rusage_system 0.013997
STAT curr_connections 13
STAT total_connections 16
STAT connection_structures 14
STAT reserved_fds 20
STAT cmd_get 113
STAT cmd_set 226
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 0
STAT get_misses 113
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 31426
STAT bytes_written 3403
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT bytes 40793
STAT curr_items 226
STAT total_items 226
STAT evictions 0
STAT reclaimed 0
END
5. 參數設定
# vi /etc/sysconfig/memcached
以下為預設參數值: 
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
6. 防火牆設定
# vi/etc/sysconfig/iptables
增加以下內容
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 11211 -j ACCEPT
7. 重新啟動 memcached server
# service memcached restart


For Linux
1. 下载memcached-1.2.1.tar.gz和libevent-1.3b.tar.gz
2. 安装libevent
# tar xvfz  libevent-1.3.tar.gz
# cd libevent-1.3
# ./configure --prefix=/usr
# make & make install
# ls -al /usr/lib | grep libevent
3. 安装 memcached
# tar xvfz  memcached-1.2.5.tar.gz
#  cd  memcached-1.2.5
# ./configure --with-libevent=/usr
#  make & make install
#  ls -al /usr/local/bin/mem*
4. 將 libevent 安裝的/usr/local/lib 輸出到涵式庫路徑
# vi ~/.bash_profile
add the following sentence
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
5.  啟動 memcached: /usr/local/bin/memcached
# memcached  -d  -m 128 -l 192.168.152.143 -p 11211 -u root

Memcached Session Manager Installation

Environments: One lighttpd web server (192.168.56.1, installation), one memcaced server (192.168.56.2, installation), and two tomcat servers (192.168.152.140 & 192.168.152.143, here) with memcached session manager packages.

Tomcat server with memcached session manager package:
1. put the following eight jar files into your tomcat library folder (e.g., /usr/share/apache-tomcat-7.0.**/lib)
  • http://memcached-session-manager.googlecode.com/files/memcached-session-manager-tc7-1.4.1.jar
  • http://spymemcached.googlecode.com/files/spymemcached-2.7.jar
  • http://memcached-session-manager.googlecode.com/files/kryo-1.03.jar
  • http://memcached-session-manager.googlecode.com/files/minlog-1.2.jar
  • http://memcached-session-manager.googlecode.com/files/reflectasm-0.9.jar
  • http://mirrors.ibiblio.org/pub/mirrors/maven2/asm/asm/3.2/asm-3.2.jar
  • http://github.com/downloads/magro/kryo-serializers/kryo-serializers-0.8.jar
  • http://memcached-session-manager.googlecode.com/files/msm-kryo-serializer-1.4.0.jar
2. add the following paragraph into context.xml (e.g., /usr/share/apache-tomcat-7.0.**/context.xml)
        <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
         memcachedNodes="n1: 192.168.56.2:11211"
         transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" />
* sometimes if you meet "com.esotericsoftware.kryo.SerializationException: Buffer limit exceeded reading object of type: java.util.concurrent.ConcurrentHashMap" problem, my solution is to change transcoderFactory from "de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" to "de.javakaffee.web.msm.JavaSerializationTranscoderFactory"
3. before restart your tomcat servers, the memcached server and web server have to been started.

Test jsp file:

<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%><%@ page import="java.net.InetAddress;" %>
<%
    String server_ip = "";
    InetAddress inetAddress = InetAddress.getLocalHost();
    server_ip = inetAddress.getHostAddress();
    String abc = (String) session.getAttribute("abc");
    String in_session = "false";
    if (abc == null)  {
        session.setAttribute("abc", "value  in session");
        abc  = "value not in session";
        in_session = "false";
    } else {
        in_session = "true";
    }
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
hello world! This is <%=server_ip%><br>
session id : <%=session.getId()%> <br>
abc value in session ? <%=in_session%><br>
abc value = <%=abc%>
</body>
</html>


Ref:
http://napmas.blogspot.com/2011/06/tomcat-and-memcached-and-session.html
http://code.google.com/p/memcached-session-manager/wiki/SetupAndConfiguration



Wednesday, December 28, 2011

Memcached Introduction / Installation / Usage / HA

1.     Memcached 原理:


2.     Memcached Server安裝
2.1 For Windows:   
                          http://www.ccvita.com/257.html
                          http://lwg2001s.iteye.com/blog/135976

3.     Memcached Client
3.1 Memcachec Java Client (By Greg Whalin, 目前最多人使用):
                          https://github.com/gwhalin/Memcached-Java-Client/wiki/ (API)
3.2  Spymemcached: http://code.google.com/p/spymemcached/ (API)
3.3  Xmemcached : http://code.google.com/p/xmemcached/ (API)

4.     其他
4.1 Repcached (使Memcached 具有fail over 功能, 侷限於 Master-Slave):
4.2  ReplCache (By jGroup, 將欲儲存的key-value廣播至每台server上)