I haven't updated my blog for a long time, I hope I can continue to write in the future, and now I find this is a great process. The use and introduction of the cache has been sorted out very early, and now it is posted as a personal note:
memecached Introduction
Many Web applications save data to an RDBMS, where the application server reads the data and displays it in the browser . However, with the increase of data volume and the concentration of access, the burden of RDBMS, database response deterioration, site display delay and other significant impact. This is the time to memcached. Memcached is a high-performance distributed memory cache server . The general purpose is to reduce the number of database accesses by caching database query results to improve the speed and scalability of dynamic Web applications.
Storage mode
memcached memcached built-in memory storage space memory Span style= "Color:rgb (51,51,51)" >memcached LRU (Least recently used) algorithm automatically removes unused cache memcached
MC & Spring
Spring Configuration:
<beanname= "Memcachedclient" class= "Net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean" destroy-method= " Shutdown "> <property name=" Servers "value=" ${vipcps_mc_s1_host}:${vipcps_mc_s1_port}${vipcps_mc_s2_ host}:${vipcps_mc_s2_port}${vipcps_mc_s3_host}:${vipcps_mc_s3_port}${vipcps_mc_s4_host}:${vipcps_mc_s4_port}${ Vipcps_mc_s5_host}:${vipcps_mc_s5_port} "></property> <!--nio connection poolsize. Use of connection pooling is not recommended for performance testing- - <property name= "ConnectionPoolSize" value= "1" ></property> <!--distributed strategy-- > <property name= "Sessionlocator" > <bean class= " Net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator "></bean> </property> </ Bean>
About Redis
Redis is a key-value storage System . Similar to memcached, it supports storing more value types, including string (string), list ( linked list ), set (set), Zset (SortedSet-ordered collection), and hash (hash type). These data types support Push/pop, Add/remove, and intersection-set and difference sets, and richer operations, and these operations are atomic. Based on this, Redis supports sorting in a variety of different ways. As with memcached, data is cached in memory to ensure efficiency. The difference is that Redis periodically writes the updated data to disk or writes the modified operation to the appended record file, and Master-slave (Master-Slave) synchronization is implemented on this basis.
Redis is a high-performance Key-value database. The emergence of Redis, to a large extent, compensates for the lack of memcached such key/value storage, in some cases can be a good complement to the relational database. It provides the python,ruby,erlang,php client, which is very convenient to use. [1]
Redis supports master-slave synchronization. Data can be synchronized from the primary server to any number of slave servers, from the server to the primary server that is associated with other slave servers. This enables Redis to perform single-layer tree replication. The data can be written intentionally or unintentionally from the disk. Because of the full implementation of the publish/subscribe mechanism, you can subscribe to a channel and receive a complete message release record from the master server when the tree is synchronized anywhere from the database. Synchronization is helpful for the scalability and data redundancy of read operations.
Storage mode
The Redis storage is divided into memory storage, disk storage , and log files, which are configured with three parameters in the configuration file. The Save seconds Updates,save configuration, indicating how many times the update operation is synchronized to the data file. This can be a combination of multiple conditions, such as the default configuration file settings, set three conditions. AppendOnly yes/no, appendonly configuration, indicates whether logging occurs after each update operation and, if not turned on, may result in data loss over a period of time when power is lost. Because the Redis itself synchronizes data files in sync with the save conditions above, some data will only exist in memory for a period of time. Appendfsync no/always/everysec, Appendfsync configuration, no indicates that the data cache of the operating system is synchronized to disk, always indicates that Fsync () is manually invoked after each update operation Writes data to disk, Everysec indicates synchronization once per second.
Redis is more of a replacement for memched.
Consistent hash (consistent hash) (server cluster)
The stored server is determined by an operation between the key value and the client representing the hash value of the value.
To resolve load balancing, the virtual server node is constructed when the server node is too small.
Resolves the following weaknesses of the simple hash:
1) When adding or deleting the city points, the update efficiency is low. When the number of storage nodes in the system increases or decreases, the mapping formula will change to hash (object)% (n±1), which will change the mapping position of all obiect, and the mapping location of the whole system data object needs to be recalculated. The system cannot properly respond to external access and will cause the system to be in a crash state.
2) Poor balance, regardless of node performance differences. Due to the improvement of hardware performance, the newly added node has better load-carrying capacity, how to improve the algorithm, so that the node performance can be better utilized.
3) lack of monotonicity. An important indicator of data distribution technology is monotonicity, which means that if some content has been allocated to the corresponding buffer by hashing, and when a new buffer is added to the system, the result of the hash should be able to ensure that the original allocated content can be mapped to the new buffer. Other buffers that are not mapped to the old buffer collection.
Redis & Spring
<!--Default--<bean id= "Jedispoolconfig" class= "Redis.clients.jedis.JedisPoolConfig" > <property name= "maxactive" value= "${redis.pool.maxactive}"/> <property name= "MaxIdle" value= "${redis.pool.maxidle}"/> <property name= "maxwait" value= "${redis.pool.maxwait}"/> <property name= "Testonborrow" value= "${redis.pool.testonborrow}"/> </bean> <bean ID = "StringRedisTemplate0" class= "Org.springframework.data.redis.core.StringRedisTemplate" P:connection-fac tory-ref= "JedisConnectionFactory0" scope= "prototype" ></bean> <bean id= "Stringrediste Mplate1 "class=" Org.springframework.data.redis.core.StringRedisTemplate "p:connection-factory-ref=" Jedis ConnectionFactory1 "scope=" prototype "></bean> <!--Custom--< Bean id= "jedisconnectionFactory0 "class=" Org.springframework.data.redis.connection.jedis.JedisConnectionFactory "> <property name= "HostName" value= "${vipcps_redis_host_0}"/> <property name= "Port" value= "$ {vipcps_redis_port_0} "/> <property name=" poolconfig "ref=" Jedispoolconfig "/> </bean > <bean id= "jedisConnectionFactory1" class= "Org.springframework.data.redis.connection.jedi S.jedisconnectionfactory "> <property name=" hostName "value=" ${vipcps_redis_host_1} "/> <property name= "Port" value= "${vipcps_redis_port_1}"/> <property name= "Poolconfig" ref= "Jedispoolconfig"/> </bean> <bean id= "Redistemplaterouter" class= "com.vipshop.unionwe B.framework.redis.redistemplaterouter "> <constructor-arg> <LIST&G T <ref bean= "StringRedisTemplate0"/> <ref bean= "Stringredistempl Ate1 "/> </list> </constructor-arg> </bean>
Java implementation
public class Redistemplaterouter {privatestatic Finallogger logger = Loggerfactory.getlogger (redistemplaterouter . Class); privatestatic CRC32 CRC32 = newCRC32 (); /** * Number of services * */privatestatic intserver_count = 1; Privatestatic list<stringredistemplate>redistemplates =newarraylist<stringredistemplate> (); Publicredistemplaterouter (list<stringredistemplate> redistemplates) {redistemplaterouter.redistemplates= Redistemplates; Server_count= redistemplates.size (); } publicstatic List<stringredistemplate>getredistemplates () {returnredistemplates; } publicstatic Stringredistemplategetrouteredis (String key) {crc32.reset (); Crc32.update (Key.getbytes ()); Longcrc32value = Crc32.getvalue (); Intindex = Math.Abs ((int) (crc32value% server_count)); Logger.debug ("Route to Redis [KEY:{},INDEX:{},CRC32:{}] ", new object[] {key, index, crc32value}); Returnredistemplates.get (index); }}
Usage:
Redistemplate template= Redistemplaterouter.getrouteredis (key); Object result = Template.opsforvalue (). get (key);
Ehcache
is a pureJavaIn -Process cache Framework;Cache in memory and disk storage can be scaled to several g;Ehcacheis a widely used open sourceJavaDistributed Cache;SupportLRU,LFUand theFIFOmultiple elimination algorithms;The ability to cache pages, objects, and data while supporting clustering/Distributed Cache;
Object Cache Spring Configuration
object caching is to add the query data to the cache, the next time you query again directly from the cache, rather than go to the database query. <!--Association Profiles--
1. <bean id= "cachemanagerfactory" class= "Org.springframework.cache.ehcache.EhCacheManagerFactoryBean" > 2. <property name= "configlocation" value= "Classpath:com/config/ehcache.xml"/> 3. </bean> 4. 5. <!--support cache annotations --6. <cache:annotation-driven /> 7. 8. <!--default is CacheManager configuration Cache Manager --9. <bean id= "CacheManager" class= "Org.springframework.cache.ehcache.EhCacheCacheManager" > . <property name= "CacheManager" ref= "Cachemanagerfactory"/> One by one . </bean>
Ehcahe Configuration Instance
<ehcacheupdatecheck= "false" monitoring= "AutoDetect" dynamicconfig= "true" > <diskstorepath= "Java.io.tmpdir "/> <defaultcache maxelementsinmemory=" 10000 "eternal=" false "timetoidleseconds= "Timetoliveseconds=" "0" overflowtodisk= "true" maxelementsondisk= "10000000" Diskpersistent= "false" diskexpirythreadintervalseconds= "memorystoreevictionpolicy=" "LRU"/& Gt <!--Channel comprehensive report Cache--<cachename= "Channelsummarycache" maxelementsinmemory= "eternal=" false " timetoidleseconds= "timetoliveseconds=" overflowtodisk= "false" maxelementsondisk= "10000000" diskPersistent = "false" diskexpirythreadintervalseconds= "memorystoreevictionpolicy=" FIFO "/> <!-- Configure Custom Cache Maxelementsinmemory: The maximum number of objects that are allowed to be created in the cache eternal: whether the object in the cache is permanent, and if so, the timeout setting is ignored and the object never expires. Timetoidleseconds: The passivation time of cached data, that is, before an element dies, The maximum time interval value for two access times, which can only be valid if the element is not permanently resident, and if the value is 0 means that the element can pause for an infinite amount of time. Timetoliveseconds: The lifetime of the cached data, that is, the maximum interval value of an element from build to extinction, which can only be valid if the element is not permanently resident, and if the value is 0 means that the element can pause for an infinite amount of time. Overflowtodisk: If the disk cache is enabled when there is not enough memory. Memorystoreevictionpolicy: Cache is full after the elimination algorithm. -
Java
The following three cache annotations are available:
@Cacheable (value= "Accountcache") , this annotation means that when this method is called, it is from a name Accountcache In the cache, if not, the actual method is executed (that is, the database is queried) and the execution results are cached, otherwise the objects in the cache are returned.
@CacheEvict comment To mark the method to empty the cache, and when the method is called, the cache is emptied.
@CachePut Note, this annotation ensures that the method is executed, and that the return value of the method is also recorded in the cache, enabling the cache to be updated synchronously with the database.
# with Parameters
@CachePut (value= "Channelsummarycache", key= "#startTime. GetTime (). toString (). Concat ('_'). Concat (' + #endTime). Concat ('_') "+". Concat (' + #commissionType). Concat ('_'). Concat (#userId). Concat (' _ ' + #channelTag) ") publiclist< Summaryreportwithchannelvo>summarygroupbychannel (Integer userid,date startTime, Date endTime, short Commissiontype, String channeltag) {...}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Mc&redis&ehcache Introduction