In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what is Memcached". In daily operation, I believe many people have doubts about what Memcached is. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what is Memcached?" Next, please follow the editor to study!
Memcached is a distributed in-memory object caching system developed by danga.com (the technical team that operates LiveJournal) to reduce database load and improve performance in dynamic systems.
Applicable occasion
1. Distributed applications. Because memcached itself is based on distributed systems, it is especially suitable for large-scale distributed systems.
two。 Database front segment cache. Database is often the bottleneck of website system. The large amount of concurrent access to the database often causes the website memory overflow. Of course, we can also use Hibernate's caching mechanism. But memcached is based on distributed, and can be independent of the website application itself, so it is more suitable for large-scale website to split applications.
3. Data sharing between servers. For example, we split the login system and query system of the website into two applications, put them on different servers and cluster them. At this time, after users log in, how to synchronize the login information from the login system server to the query system server? At this point, we can use memcached, the login system will cache the login information, and the query system can get the login information, just like getting local information.
Inapplicable situation
For applications that do not need to be "distributed", shared, or simply small enough to have only one server, memcached will not bring any benefits, on the contrary, it will slow down the efficiency of the system, because network connections also require resources.
Installation
The installation of the windows environment is introduced here.
1. Download the windows stable version of memcache, extract it and put it under a disk, such as c:\ memcached
two。 Enter c:\ memcached\ memcached.exe-d install installation under cmd
3. Then enter: C:\ memcached\ memcached.exe-d start to start.
In the future, memcached will start automatically every time you boot as a service of windows. So the server side has been installed.
Client
Memcached itself is developed in C, and the client can be php, C #, or java.
There are two java-based clients on the Internet:
1.javamemcached-release2.6.3
Introduction to ①
This is a more general Memcached client framework. The specific originality is unknown.
Jar on which ② depends
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
Introduction to ①
This thing is packaged by Cen Wenchu, an architect of Ali Software. The notes inside are all in Chinese, which is better.
Jar on which ② depends
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
Example
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 String 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 / 3030); 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 data into cache client.set ("test2", "test2"); / / put data into cache and set 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
Configure memcached.xml
Com.alisoft.xplatform.asf.cache.memcached.MemcachedErrorHandler 127.0.0.1:11211
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 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 a custom java bean. But it must be a serializable java bean (implements Serializable is fine)
Based on javamemcached-release2.6.3
Java bean for testing
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 method slightly}
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 String 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 / 3030); 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 data into 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 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 list=new ArrayList (); list.add (bean); cache.put ("beanList", list); List listClient= (List) cache.get ("beanList"); if (listClient.size () > 0) {TestBean bean4List=listClient.get (0); System.out.println (bean4List.getName ());}} finally {manager.stop () At this point, the study of "what is Memcached" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.