I. brief introduction:
The cache solution in memcache cluster environment is a high-performance distributed memory object cache system. By maintaining a unified and huge hash table in the memory database, it stores all kinds of data, including images, videos, files and database retrieval results.
Simply speaking, data is called to storage and then read from memory, which greatly improves the reading speed. memcache is the name of this project, and memcached is the main program file name of its server.
2. Common methods:
1. Initialize memcache
//Initialize memcache public static void initMemcache(){ String[] servers = {"127.0.0.1:11211"}; SockIOPool pool = SockIOPool.getInstance(); pool.setServers(servers); pool.setFailover(true); pool.setInitConn(10); pool.setMinConn(5); pool.setMaxConn(250); pool.setMaintSleep(30); pool.setNagle(false); pool.setSocketTO(3000); pool.setAliveCheck(true); pool.initialize(); }
2. Create cache
/** * Create cache * @param key key * @param value value */ public static void createMemcache(String key, Object value){ //Create client object MemCachedClient mclent = new MemCachedClient(); //Create cache mclent.set(key, value); }
3. Delete the specified cache
/** * Delete specified cache * @param key key */ public static void deleteMemcache(String key){ MemCachedClient mclent = new MemCachedClient(); mclent.delete(key); }
4. Get cache
/** * Get cache * @param key key * @return value */ public static Object getMemcache(String key){ MemCachedClient mclent = new MemCachedClient(); Object value = mclent.get(key); return value; }
5. Get multiple caches
/** * Get multiple cache objects * @param keys * @return */ public static Map<String, Object> getManyMemcache(String[] keys){ MemCachedClient mclent = new MemCachedClient(); Map<String, Object> m = mclent.getMulti(keys); return m; }
6. Clear the specified type of cache
/** * Clear cache of the specified type * @param str * @return */ public static boolean flashLMemcache(String[] str){ MemCachedClient mclent = new MemCachedClient(); mclent.flushAll(str); return true; }
7. Clear all caches
/** *wipe cache * @return */ public static boolean flashMemcache(){ MemCachedClient mclent = new MemCachedClient(); mclent.flushAll(); return true; }
III. demonstration and test:
/** * memcache,Store call test */ AbMemcache.initMemcache(); AbMemcache.createMemcache("redant", "Today, 20181106"); Object obj = AbMemcache.getMemcache("redant"); System.err.println(obj);
Results:
/**
- memcache, delete the specified cache
*/
AbMemcache.deleteMemcache("redant");
System.err.println(AbMemcache.getMemcache("redant"));
/** - Clear all cache
*/
AbMemcache.flashMemcache();
System.err.println(AbMemcache.getMemcache("redant"));
Results: