Simple application of memcached

Simple application of memcached


1. Memcached is a distributed memory object caching system developed by danga.com (the technical team operating LiveJournal), which is used to reduce database load and improve performance in dynamic systems.
2. A method of data caching based on the key-value method. In the memory where the data is stored, only string storage is supported. We can convert the data model through serialization (JSON string) and deserialization (data model).
3. Memcached itself is developed using C, and the client can be php, C#, or java. I do java, so I only introduce java-based clients here.
4. Memcached itself has the function of accessing objects, but it will judge the package name of the object, and cannot be converted if the package path is different (the field parameters are the same).


example:
public static void main(String[] args) {
        MemCachedClient client=new MemCachedClient();
        String [] addr ={"127.0.0.1:11211"};
        Integer [] weights = {3};
        SockIOPool pool = SockIOPool.getInstance();
        pool.setServers(addr); //memcached server address configuration
        pool.setWeights(weights);//memcached server weight configuration
        pool.setInitConn(5); //Number of connections established for each server during initialization
        pool.setMinConn(5); //The minimum number of connections established by each server, when the self-check thread finds that the number of connections established with a server is less than this number, it will make up for the remaining connections
        pool.setMaxConn(200); //The maximum number of connections established by each server, when the self-check thread finds that the number of connections established with a server is greater than this number
    				//When the idle time of these connections is checked one by one, whether the idle time is greater than maxConn, if it is greater than maxConn, these connections will be closed until the number of connections is equal to maxConn
        pool.setMaxIdle(1000*30*30);//Maximum idle time
        pool.setMaintSleep(30); //Self-check thread cycle to work, each sleep time
        pool.setNagle(false); //Socket parameter, if it is true, it will not buffer when writing data, and send it immediately
        pool.setSocketTO(30); //Socket blocking timeout for reading data
        pool.setSocketConnectTO(0); //Socket blocking waiting time for establishing a connection
        pool.initialize();

//      String [] s  =pool.getServers();
        client.setCompressEnable(true); //Set whether to compress the data put in the cache, the default value is true
        client.setCompressThreshold(1000*1024);//Set the threshold of cache data to be compressed, the default value is 30k

// put data into cache
        client.set("test2","test2");

// Put the data into the cache and set the expiration time
        Date date=new Date(2000000);
        client.set("test1","test1", date);

// delete cached data
//      client.delete("test1");

// get cached data
        String str =(String)client.get("test1");
        System.out.println(str);
    }
}



1. SockIOPool is a monomorphic method that establishes a connection and puts it in the connection pool, which is convenient for later use and reduces the time to be established when using it.
2. This method has an overloaded method getInstance( String poolName ), and only one SockIOPool instance is constructed for each poolName. The default constructed poolName is default. If you configure multiple memcached services on the client, be sure to explicitly declare the poolName.
3. How to hook up MemCachedClient and SockIOPool
pool = SockIOPool.getInstance("SP");
MemCachedClient client = new MemCachedClient("SP" );
//This is linked by name.


Refer to original text: http://blog .csdn.net/qqiabc521/article/details/6438429
refer to the original text: http://www.cnblogs.com/han1982/p/4125527.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327033285&siteId=291194637