Data cache--Detailed explanation of Memcach and how to use it (with code)?

Before using Memcach, what is Memcach and Memcached?   

A. What is Memcache?

        Memcache is a high-performance distributed memory object caching system. By maintaining a unified huge hash table in memory, it can be used to store data in various formats, including images, videos, files, and database retrieval results. . Simply put, the data is called into the memory, and then read from the memory, thereby greatly improving the reading speed. Memcached is a high-concurrency in-memory key-value pair cache system. Its main function is to cache database query results, content, and other time-consuming calculation results in system memory, thereby accelerating the response speed of web applications.

B. Difference between Memcache and memcached?

   In fact, Memcache is the name of this project, and memcached is the main program file name on the server side, you know what I mean. One is the project name and the other is the main program file name. I saw that many people didn't understand it on the Internet, so they used it together.

1. Installation of Memcached [The installation package in the file below can be downloaded directly]

   Before version 1.4.5, memcached could be installed as a service, but this feature was removed in later versions. Therefore, the installation of memcached can be divided into two categories, the first is the version before 1.4.5, and the other is the version after 1.4.5.

  1) Install memcached < 1.4.5:

       1. Unzip the downloaded file to any directory.

       2. Versions of memcached prior to 1.4.5 will be installed as a service, open the console as administrator, and run the following command:

          c:\memcached\memcached.exe -d install

        * Note to replace the path c:\memcached\memcached.exe with your local installation path.

       3. Then use the following commands to start or stop the memcached service:

          c:\memcached\memcached.exe -d start

          c:\memcached\memcached.exe -d stop

       4. For example, if you want to increase the maximum memory limit used by memcached, you can modify the value of ImagePath:

          "c:\memcached\memcached.exe" -d runservice -m 512

         * In addition to the parameter '-m 512', you can also use other parameters. All available parameters can be viewed through "c:\memcached\memcached.exe -h".

       5. If you want to uninstall the memcached service, you can use the following command:

          c:\memcached\memcached.exe -d uninstall

    2) Install memcached >= 1.4.5

        1. Unzip the downloaded file to any directory.

        2. Memcached versions after 1.4.5 cannot run as a Windows service, you can start memcache on the command line. Open cmd as administrator and enter the following command:

                                        Memcached-x86 –p 11211 –m 128 –vv

        3. The memcached startup options are as follows:

         Option Description

          -p TCP port to use, default is 11211

         -m The maximum memory size defaults to 64m

         -vv start in very verbose mode, output debugging information and errors to the console

         -d start as a daemon in the background

      4. How to use Memcache in java

         1. Add a jar package: [There are jar packages in the file below]

             commons-pool-1.5.6.jar、

             java_memcached-release_2.6.6.jar、

             slf4j-api-1.6.1.jar、

2. Add the auxiliary class MemcachedUtil.java [copy and paste directly into your package (eg: com.wf.ssm.util)]

    import java.util.Date;

    import com.danga.MemCached.MemCachedClient;

    import com.danga.MemCached.SockIOPool;

 

    public class MemcachedUtil {

 

/**

     * memcached client singleton

     */

    private static MemCachedClient cachedClient = new MemCachedClient();

     

    /**

     * Initialize the connection pool

     */

    static {

        //Get an instance of the connection pool

        SockIOPool pool = SockIOPool.getInstance();

         

        //list of servers and their weights

        String[] servers = {"127.0.0.1:11211"};

        Integer[] weights = {3};

         

        //set server information

        pool.setServers(servers);

        pool.setWeights(weights);

         

        //Set the initial number of connections, the minimum number of connections, the maximum number of connections, and the maximum processing time

        pool.setInitConn(10);

        pool.setMinConn(10);

        pool.setMaxConn(1000);

        pool.setMaxIdle(1000*60*60);

         

        //Set the sleep time of the connection pool daemon thread

        pool.setMaintSleep(60);

         

        //Set TCP parameters, connection timeout

        pool.setNagle(false);

        pool.setSocketTO(60);

        pool.setSocketConnectTO(0);

         

        //Initialize and start the connection pool

        pool.initialize();

         

        //Compression settings, all files that exceed the specified size are compressed

        //      cachedClient.setCompressEnable(true);

        //      cachedClient.setCompressThreshold(1024*1024);

    }

     

    private MemcachedUtil(){

    }

     

    public static boolean add(String key, Object value) {

        return cachedClient.add(key, value);

    }

     

    public static boolean add(String key, Object value, Date expireDate) {

        return cachedClient.add(key, value, expireDate);

    }

    

    public static boolean add(String key, Object value, Integer millSeconds){

    return cachedClient.add(key, value, new Date(new Date().getTime()+millSeconds));

    }

     

    public static boolean put(String key, Object value) {

        return cachedClient.set(key, value);

    }

     

    public static boolean put(String key, Object value, Date expireDate) {

        return cachedClient.set(key, value, expireDate);

    }

    

    public static boolean put(String key, Object value, Integer millSeconds){

    return cachedClient.set(key, value, new Date(new Date().getTime()+millSeconds));

    }

     

    public static boolean replace(String key, Object value) {

        return cachedClient.replace(key, value);

    }

     

    public static boolean replace(String key, Object value, Date expireDate) {

        return cachedClient.replace(key, value, expireDate);

    }

    

    public static boolean replace(String key, Object value, Integer millSeconds){

    return cachedClient.replace(key, value, new Date(new Date().getTime()+millSeconds));

    }

     

    public static Object get(String key) {

        return cachedClient.get(key);

    }

    

   public static Object delete(String key) {

       return cachedClient.delete(key);

   }

  }

 

3. Note that the class added to the cache must implement the interface Serializable.

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326317067&siteId=291194637