About Memcached _ Power node Java College Collation

  • 2020-06-19 12:24:37
  • OfStack

Memcached is a distributed memory object caching system developed by danga. com (the technical team that runs LiveJournal) to reduce database load and improve performance in dynamic systems.

Applicable occasions

1. Distributed applications. Since memcached itself is based on distributed systems, it is particularly suitable for large distributed systems.

2. Front cache of database. The database is often the bottleneck of the website system. The large amount of concurrent access to the database often causes the site memory overflow. Of course, we can also use Hibernate's caching mechanism. memcached, however, is distributed and independent of the web application itself, making it more suitable for large web sites to split applications.

3. Data sharing between servers. For example, we split the login system and query system of the website into two applications, which are placed on different servers and clustered. How can the login information be synchronized from the login system server to the query system server after the user logs in? At this point, we can use memcached. The login system will cache the login information and query the system to get the login information, just like getting local information 1.

Inapplicable occasions

For applications that don't need to be "distributed," or Shared, or even small enough to have only one server, memcached doesn't do any good and instead slows down system efficiency because network connections also require resources

The installation

Here's how to install the windows environment.

1. Download the windows stable version of memcache and unzip it under a disk, such as c:\memcached

2. Enter c:\memcached\memcached. exe-d install installation under cmd

3. Reenter: c:\memcached\ memcached. exe-ES57en start start.

In the future, memcached will start automatically as one of windows's services every time it is turned on. The server side is now installed.

The client

Memcached itself was developed using C and the client can be php, C#, or java.

There are two java based clients on the Internet:

1.javamemcached-release2.6.3

(1) introduction

This is the common Memcached client framework. The specific original is unknown.
Dependent on jar

commons-pool-1.5.6.jar javamemcached-release2.6.3.jar slf4j-api-1.6.1.jar slf4j-simple-1.6.1.jar

2.alisoft-xplatform-asf-cache-2.5.1

(1) introduction

This east east is the architect of Ali software Cen Wenchu to encapsulate. The notes are in Chinese, which is better.

Rely on jar

alisoft-xplatform-asf-cache-2.5.1.jar commons-logging-1.0.4.jar hessian-3.0.1.jar log4j-1.2.9.jar stax-api-1.0.1.jar wstx-asl-2.0.2.jar

sample

Based on javamemcached - release2. 6.3


package com.bjpowernode.memcached.cache;

import java.util.Date;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;

public class MyCache {
  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);
    pool.setWeights(weights);
    pool.setInitConn(5);
    pool.setMinConn(5);
    pool.setMaxConn(200);
    pool.setMaxIdle(1000*30*30);
    pool.setMaintSleep(30);
    pool.setNagle(false);
    pool.setSocketTO(30);
    pool.setSocketConnectTO(0);
    pool.initialize();

//   String [] s =pool.getServers();
    client.setCompressEnable(true);
    client.setCompressThreshold(1000*1024);

//    Put the data into the 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);
  }
}

Based on alisoft xplatform - asf cache -- 2.5.1

Configuration memcached xml


<?xml version="1.0" encoding="UTF-8"?>
<memcached>
  <!-- name  Properties are used in programs Cache The only 1 logo ;socketpool  The attribute will be associated with the following socketpool configuration ; -->
  <client name="mclient_0" compressEnable="true" defaultEncoding="UTF-8"
    socketpool="pool_0">
    <!--  Optional, used to handle error situations  -->
    <errorHandler>com.alisoft.xplatform.asf.cache.memcached.MemcachedErrorHandler
    </errorHandler>
  </client>

  <!--
    name  Properties and client  The configuration of the socketpool  Attributes are associated. 
    maintSleep The attribute is background thread management SocketIO Pool check interval, if set to 0 , indicates that background thread maintenance is not required SocketIO Thread pools that need to be managed by default. 
    socketTO  Attributes are Socket Operation timeout configuration, units ms .  aliveCheck
     Attribute indicates that it is in use Socket Whether to check first before Socket State. 
  -->
  <socketpool name="pool_0" maintSleep="5000" socketTO="3000"
    failover="true" aliveCheck="true" initConn="5" minConn="5" maxConn="250"
    nagle="false">
    <!--  Set up the memcache Server instance address . Multiple address "," separated  -->
    <servers>127.0.0.1:11211</servers>
    <!--
       Optional configuration. Represents the server instance set up above Load The weight .  For example,  <weights>3,7</weights>  said 30% load  in 
      10.2.224.36:33001, 70% load  in  10.2.224.46:33001

    <weights>3,7</weights>
    -->
  </socketpool>
</memcached>

The test class


package com.bjpowernode.memcached.client.test;

import java.util.ArrayList;
import java.util.List;

import com.alisoft.xplatform.asf.cache.ICacheManager;
import com.alisoft.xplatform.asf.cache.IMemcachedCache;
import com.alisoft.xplatform.asf.cache.memcached.CacheUtil;
import com.alisoft.xplatform.asf.cache.memcached.MemcachedCacheManager;
import com.bjpowernode.memcached.cache.client.TestBean;

public class ClientTest {

  @SuppressWarnings("unchecked")
  public static void main(String[] args) {
    ICacheManager<IMemcachedCache> manager;
    manager = CacheUtil.getCacheManager(IMemcachedCache.class,
        MemcachedCacheManager.class.getName());
    manager.setConfigFile("memcached.xml");
    manager.start();
    try {
      IMemcachedCache cache = manager.getCache("mclient_0");
      cache.put("key", "value");
      System.out.println(cache.get("key"));
    } finally {
      manager.stop();
    }
  }

}

Use memcached to cache java bean custom objects

Memcached can cache String or custom java bean. But it must be serializable java bean (implements Serializable will do)
Based on javamemcached - release2. 6.3

java bean was used for the test


package com.bjpowernode.memcached.cache.client;

import java.io.Serializable;

public class TestBean implements Serializable{
  private static final long serialVersionUID = 5344571864700659321L;

  private String name;
  private Integer age;
  //get , set Methods a little 
}

MyCache java code


package com.bjpowernode.memcached.cache;

import java.util.Date;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;

public class MyCache {
  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);
    pool.setWeights(weights);
    pool.setInitConn(5);
    pool.setMinConn(5);
    pool.setMaxConn(200);
    pool.setMaxIdle(1000*30*30);
    pool.setMaintSleep(30);
    pool.setNagle(false);
    pool.setSocketTO(30);
    pool.setSocketConnectTO(0);
    pool.initialize();

//   String [] s =pool.getServers();
    client.setCompressEnable(true);
    client.setCompressThreshold(1000*1024);

//    Put the data into the cache 
    TestBean bean=new TestBean();
    bean.setName("name1");
    bean.setAge(25);
    client.add("bean1", bean);

//    Get cached data 
    TestBean beanClient=(TestBean)client.get("bean1");
    System.out.println(beanClient.getName());
  }
}

Based on alisoft xplatform - asf cache -- 2.5.1


package com.bjpowernode.memcached.client.test;

import java.util.ArrayList;
import java.util.List;

import com.alisoft.xplatform.asf.cache.ICacheManager;
import com.alisoft.xplatform.asf.cache.IMemcachedCache;
import com.alisoft.xplatform.asf.cache.memcached.CacheUtil;
import com.alisoft.xplatform.asf.cache.memcached.MemcachedCacheManager;
import com.bjpowernode.memcached.cache.client.TestBean;

public class ClientTest {

  @SuppressWarnings("unchecked")
  public static void main(String[] args) {
    ICacheManager<IMemcachedCache> manager;
    manager = CacheUtil.getCacheManager(IMemcachedCache.class,
        MemcachedCacheManager.class.getName());
    manager.setConfigFile("memcached.xml");
    manager.start();
    try {
      IMemcachedCache cache = manager.getCache("mclient_0");

      TestBean bean=new TestBean();
      bean.setName("name1");
      bean.setAge(25);
      cache.put("bean", bean);
      TestBean beanClient=(TestBean)cache.get("bean");
      System.out.println(beanClient.getName());

      List<TestBean> list=new ArrayList<TestBean>();
      list.add(bean);
      cache.put("beanList", list);
      List<TestBean> listClient=(List<TestBean>)cache.get("beanList");
      if(listClient.size()>0){
        TestBean bean4List=listClient.get(0);
        System.out.println(bean4List.getName());
      }
    } finally {
      manager.stop();
    }
  }

}


Related articles: