Memcache parameter settings

Source: Internet
Author: User
Memcached Java client API details

For memcached official website to provide the java_memcached-release_2.0.1 version of read analysis, memcached Java client lib library mainly provides the call class is sockiopool and memcachedclient ?, The key classes and methods are described as follows.

Sockiopool

This class is used to create a management client and server communication connection pool. The main work of the client includes data communication, server locating, and hash code generation.

  • Public static sockiopool getinstance ()
    • Obtain the single-state method of the connection pool. This method has an overload method getinstance (string poolname). Each poolname constructs only one sockiopool instance. The default poolname is default.
    • If you configure multiple memcached services on the client, you must explicitly declare the poolname.
  • Public void setservers (string [] servers)
    • Sets the list of available cache servers in the connection pool. The server consists of IP: Port (for example, 127.0.0.1: 11211)
  • Public void setweights (integer [] weights)
    • Set the weights of available cache servers in the connection pool, which correspond to the locations of server arrays one by one.
    • The implementation method is to place the same number of servers (as shown in the code below) in the bucket of the Connection Pool Based on each weight. Therefore, the maximum public approx value of the ownership weight should be 1, otherwise, the bucket resources will be wasted.
for ( int i = 0; i < servers.length; i+/+ ) { if ( this.weights /!= null && this.weights.length > i ) { for ( int k = 0; k < this.weights[i].intValue(); k+/+ ) { this.buckets.add( servers[i] ); if ( log.isDebugEnabled() ) log.debug( "++++ added " + servers[i] + " to server bucket" ); } }
  • Public void setinitconn (INT initconn)
    • Set the number of available connections for each cache server at the beginning.
  • Public void setminconn (INT minconn)
    • Sets the minimum number of available connections for each server.
  • Public void setmaxconn (INT maxconn)
    • Set the maximum number of available connections for each server
  • Public void setmaxidle (long maxidle)
    • Sets the maximum wait time for an available connection pool.
  • Public void setmaintsleep (long maintsleep)
    • Set the sleep time of the connection pool maintenance thread
    • Set to 0. The maintenance thread does not start.
    • The maintenance thread outputs the running status of the socket through log, and monitors the number of connections and idle waiting time to control connection creation and shutdown.
  • Public void setnagle (Boolean Nagle)
    • Set whether to use the Nagle algorithm. Because our communication data volume is usually large (relative to TCP Control Data) and response is required in a timely manner, this value must be set to false (the default value is true)
  • Ublic void setsocketto (INT socketto)
    • Set the socket read wait timeout value
  • Public void setsocketconnectto (INT socketconnectto)
    • Set the socket connection wait timeout value
  • Public void setalivecheck (Boolean alivecheck)
    • Set the connection heartbeat monitoring switch.
    • If it is set to true, connection monitoring is required for each communication, resulting in a doubling of the number of communications and increased network load. Therefore, this parameter should be set to true when ha requirements are relatively high, the default status is false.
  • Public void setfailback (Boolean failback)
    • Set connection failure recovery Switch
    • If this parameter is set to true, the socket connection can continue to be used after the server is started or the network connection is interrupted. Otherwise, the socket connection will not be used. The default value is true. We recommend that you keep the default value.
  • Public void setfailover (Boolean failover)
    • Set Fault Tolerance Switch
    • Set to true. When the current socket is unavailable, the program automatically finds available connections and returns the result. Otherwise, null is returned. The default status is true. We recommend that you keep the default value.
  • Public void sethashingalg (int alg)
    • Set Hash Algorithm
      • ALG = 0 use string. hashcode () to obtain the hash code. This method depends on JDK and may be incompatible with other clients. We recommend that you do not use
      • ALG = 1 use original compatible with the hash algorithm, compatible with other clients
      • ALG = 2 Use CRC32 compatible with the hash algorithm, compatible with other clients, and superior performance to the original algorithm
      • ALG = 3 use MD5 Hash Algorithm
    • When the first three hash algorithms are used, the remainder method is used to find the cache server. Use the consistent method when using the last hash algorithm to find the cache service.
  • Public void initialize ()
    • After setting the pool parameter, call this method to start the pool.
Memcachedclient?
  • Public void setcompressenable (Boolean compressenable)
    • Set whether to compress data into the cache
    • The default value is true.
    • If this value is set to true, do you need to set compressthreshold?
  • Public void setcompressthreshold (long compressthreshold)
    • Set the threshold of cache data to be compressed
    • The default value is 30 kb.
  • Public void setprimitiveasstring (Boolean primitiveasstring)
    • Set the original cache data type to string.
    • The default value is false.
    • It is set to true only when the cache data type is string, which can speed up processing.
  • Public void setdefaultencoding (string defaultencoding)
    • Encoding format used when primitiveasstring is true
    • The default value is UTF-8.
    • If you confirm that the data is mainly written to non-ASCII characters such as Chinese characters, we recommend that you use a shorter encoding format such as GBK.
  • Cache data write operation method
    • Set Method
      • Save the data to the cache server. If the data is successfully saved, true is returned.
      • If the cache server has the same key, replace it
      • Set has five overload methods. Key and value are required parameters, and there are three optional parameters: expiration time, hash code, and whether the value is a string.
    • Add Method
      • Add data to the cache server. If the data is successfully saved, true is returned.
      • If the cache server has the same key, false is returned.
      • Add has four overload methods. Key and value are required parameters, and expiration time and hash code are optional parameters.
    • Replace Method
      • Replace the data with the same key on the cache server. If the data is successfully saved, true is returned.
      • If the cache server does not have the same key, false is returned.
      • Replace has four overload methods. Key and value are required parameters, and expiration time and hash code are optional parameters.
    • We recommend that you analyze the key rules. If there is a regular order, you can construct your own hash code to improve storage efficiency.
  • How to read cache data
    • Use the get method to obtain a data from the Cache Server
      • If the data is compressed or serialized during write, the get results are automatically decompressed and deserialized.
      • The get method has three overload methods. The key is a required parameter, and the hash code and value are optional parameters.
    • Use the getmulti method to obtain a set of data from the Cache Server
      • Get method array implementation, the input parameter keys is a key Array
      • Returns a map.
  • Use counters through Cache
    • Use the storecounter method to initialize a counter
    • Increment counter operations using incr
    • Use decr to reduce the number of counters

Memcache parameter settings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.