首页 > 代码库 > memcached的使用

memcached的使用

 1. 下载memcached客户端文件

把下载文件放在文件目录下

2. 安装 memcached

命令: c:/memcached/memcached.exe  -d install

3. 启动 memcached

 c:/memcached/memcached.exe -d  start 

4. 连接到 memcached

telnet localhost 11211

5:客户端使用的基本命令

启动/结束
memcached -d -m 10 -u root -l 127.0.0.1 -p 11200 -c 256 -P /tmp/memcached.pid
-d 选项是启动一个守护进程
-m 是分配给Memcache使用的内存数量,单位是MB,这里是10MB
-u 是运行Memcache的用户,这里是root
-l 是监听的服务器IP地址,如果有多个地址的话,这里指定了服务器的IP地址127.0.0.1 
-p 是设置Memcache监听的端口,这里设置了12000,最好是1024以上的端口
-c 选项是最大运行的并发连接数,默认是1024,这里设置了256,按照你服务器的负载量来设定
-P 是设置保存Memcache的pid文件
kill `cat /tmp/memcached.pid`  关闭memcached

6:在项目中使用(与Spring集成)

获取Memcached API 地址 :

创建接口MemcachedClient  

 1  import java.util.Collection;  2 import java.util.Map;  3 import java.util.concurrent.TimeoutException;  4   5 /**  6  * 提供通用的memcached访问接口  7  */  8 public interface MemcachedClient {  9     /** 10      * 不管数据存在不存在都会将目前设置的数据存储的memcached 11      *  12      * @param key 13      *            不要超过250字节 14      * @param exp 15      *            以秒为单位,如果超过30天的值,将会用unix timestamp来制定截止有效时间 16      * @param value 17      * @return 18      * @throws TimeoutException 19      * @throws InterruptedException 20      * @throws MemcachedClientException 21      */ 22     boolean set(String key, int exp, Object value) throws TimeoutException, 23             InterruptedException, MemcachedClientException; 24  25     /** 26      * 不管数据存在不存在都会将目前设置的数据存储的memcached,自行制定超时时间 27      *  28      * @see #set(String, int, Object) 29      * @param key 30      * @param exp 31      * @param value 32      * @param timeout 33      * @return 34      * @throws TimeoutException 35      * @throws InterruptedException 36      * @throws MemcachedClientException 37      */ 38     boolean set(String key, int exp, Object value, long timeout) 39             throws TimeoutException, InterruptedException, 40             MemcachedClientException; 41  42     /** 43      * 不管数据存在不存在都会将目前设置的数据存储的memcached,但不等待返回确认 44      *  45      * @see #set(String, int, Object) 46      * @param key 47      * @param exp 48      * @param value 49      * @throws InterruptedException 50      * @throws MemcachedClientException 51      */ 52     void setWithNoReply(String key, int exp, Object value) 53             throws InterruptedException, MemcachedClientException; 54  55     /** 56      * 只有数据不存在时进行添加 57      *  58      * @param key 59      *            不要超过250字节 60      * @param exp 61      *            以秒为单位,如果超过30天的值,将会用unix timestamp来制定截止有效时间 62      * @param value 63      * @return 64      * @throws TimeoutException 65      * @throws InterruptedException 66      * @throws MemcachedClientException 67      */ 68     boolean add(String key, int exp, Object value) throws TimeoutException, 69             InterruptedException, MemcachedClientException; 70  71     /** 72      * 只有数据不存在时进行添加 73      *  74      * @param key 75      * @param exp 76      *            以秒为单位,如果超过30天的值,将会用unix timestamp来制定截止有效时间 77      * @param value 78      * @param timeout 79      *            1/1000秒为单位,在制定的时间段内没有响应就抛出TimeoutException 80      * @return 81      * @throws TimeoutException 82      * @throws InterruptedException 83      * @throws MemcachedClientException 84      */ 85     boolean add(String key, int exp, Object value, long timeout) 86             throws TimeoutException, InterruptedException, 87             MemcachedClientException; 88  89     /** 90      * 只有数据不存在时进行添加,不需要返回具体的确认信息,大多数情况下可以用此方法来提升效率,毕竟我们对Memcached的理解是假设数据不是持久的 91      *  92      * @param key 93      * @param exp 94      * @param value 95      * @throws InterruptedException 96      * @throws MemcachedClientException 97      */ 98     void addWithNoReply(String key, int exp, Object value) 99             throws InterruptedException, MemcachedClientException;100 101     /**102      * 只有数据存在才进行覆盖103      * 104      * @param key105      * @param exp106      * @param value107      * @return108      * @throws TimeoutException109      * @throws InterruptedException110      * @throws MemcachedClientException111      */112     boolean replace(String key, int exp, Object value) throws TimeoutException,113             InterruptedException, MemcachedClientException;114 115     /**116      * 只有数据存在才进行覆盖117      * 118      * @param key119      * @param exp120      * @param value121      * @param timeout122      * @return123      * @throws TimeoutException124      * @throws InterruptedException125      * @throws MemcachedClientException126      */127     boolean replace(String key, int exp, Object value, long timeout)128             throws TimeoutException, InterruptedException,129             MemcachedClientException;130 131     /**132      * 只有数据存在才进行覆盖,不等返回确认133      * 134      * @param key135      * @param exp136      * @param value137      * @throws InterruptedException138      * @throws MemcachedClientException139      */140     void replaceWithNoReply(String key, int exp, Object value)141             throws InterruptedException, MemcachedClientException;142 143     /**144      * 追加到当前数据的后面145      * 146      * @param key147      * @param value148      * @return149      * @throws TimeoutException150      * @throws InterruptedException151      * @throws MemcachedClientException152      */153     boolean append(String key, Object value) throws TimeoutException,154             InterruptedException, MemcachedClientException;155 156     /**157      * 追加到当前数据的后面158      * 159      * @param key160      * @param value161      * @param timeout162      * @return163      * @throws TimeoutException164      * @throws InterruptedException165      * @throws MemcachedClientException166      */167     boolean append(String key, Object value, long timeout)168             throws TimeoutException, InterruptedException,169             MemcachedClientException;170 171     /**172      * 追加到当前数据的后面,不等待响应173      * 174      * @param key175      * @param value176      * @throws InterruptedException177      * @throws MemcachedClientException178      */179     void appendWithNoReply(String key, Object value)180             throws InterruptedException, MemcachedClientException;181 182     /**183      * 追加到当前数据的前面184      * 185      * @param key186      * @param value187      * @return188      * @throws TimeoutException189      * @throws InterruptedException190      * @throws MemcachedClientException191      */192     boolean prepend(String key, Object value) throws TimeoutException,193             InterruptedException, MemcachedClientException;194 195     /**196      * 追加到当前数据的前面197      * 198      * @param key199      * @param value200      * @param timeout201      * @return202      * @throws TimeoutException203      * @throws InterruptedException204      * @throws MemcachedClientException205      */206     boolean prepend(String key, Object value, long timeout)207             throws TimeoutException, InterruptedException,208             MemcachedClientException;209 210     /**211      * 追加到当前数据的前面,不等待返回212      * 213      * @param key214      * @param value215      * @throws InterruptedException216      * @throws MemcachedClientException217      */218     void prependWithNoReply(String key, Object value)219             throws InterruptedException, MemcachedClientException;220 221     long incr(String key, long delta, long initValue) throws TimeoutException,222             InterruptedException, MemcachedClientException;223 224     long incr(String key, long delta, long initValue, long timeout)225             throws TimeoutException, InterruptedException,226             MemcachedClientException;227 228     void incrWithNoReply(String key, long delta) throws InterruptedException,229             MemcachedClientException;230 231     long decr(String key, long delta, long initValue) throws TimeoutException,232             InterruptedException, MemcachedClientException;233 234     long decr(String key, long delta, long initValue, long timeout)235             throws TimeoutException, InterruptedException,236             MemcachedClientException;237 238     void decrWithNoReply(String key, long delta) throws InterruptedException,239             MemcachedClientException;240 241     boolean delete(String key) throws TimeoutException, InterruptedException,242             MemcachedClientException;243 244     boolean delete(String key, long opTimeout) throws TimeoutException,245             InterruptedException, MemcachedClientException;246 247     boolean delete(String key, long cas, long opTimeout)248             throws TimeoutException, InterruptedException,249             MemcachedClientException;250 251     void deleteWithNoReply(String key) throws InterruptedException,252             MemcachedClientException;253 254     <T> T get(String key) throws TimeoutException, InterruptedException,255             MemcachedClientException;256 257     <T> T get(String key, long timeout) throws TimeoutException,258             InterruptedException, MemcachedClientException;259 260     <T> Map<String, T> get(Collection<String> keyCollections)261             throws TimeoutException, InterruptedException,262             MemcachedClientException;263 264     <T> Map<String, T> get(Collection<String> keyCollections, long timeout)265             throws TimeoutException, InterruptedException,266             MemcachedClientException;267 268     <T> GetsResponse<T> gets(String key) throws TimeoutException,269             InterruptedException, MemcachedClientException;270 271     <T> GetsResponse<T> gets(String key, long timeout) throws TimeoutException,272             InterruptedException, MemcachedClientException;273 274     <T> Map<String, GetsResponse<T>> gets(Collection<String> keyCollections)275             throws TimeoutException, InterruptedException,276             MemcachedClientException;277 278     <T> Map<String, GetsResponse<T>> gets(Collection<String> keyCollections,279             long timeout) throws TimeoutException, InterruptedException,280             MemcachedClientException;281     282     283     284     boolean isEnabled();285 }

 

创建接口实现类MemcachedClientWrapper 
  1 package cn.edu.jszg.cache;  2   3 import java.util.Collection;  4 import java.util.HashMap;  5 import java.util.Map;  6 import java.util.Set;  7 import java.util.concurrent.TimeoutException;  8   9 import net.rubyeye.xmemcached.exception.MemcachedException; 10  11 public class MemcachedClientWrapper implements MemcachedClient { 12     private net.rubyeye.xmemcached.MemcachedClient client; 13     private boolean enabled = false; 14  15     public MemcachedClientWrapper() { 16         super(); 17     } 18  19     public void setMemcachedClient(net.rubyeye.xmemcached.MemcachedClient client) { 20         this.client = client; 21     } 22  23     public net.rubyeye.xmemcached.MemcachedClient getXMemcachedClient() { 24         return client; 25     } 26  27     @Override 28     public boolean set(String key, int exp, Object value) throws TimeoutException, InterruptedException, 29             MemcachedClientException { 30         try { 31             return client.set(key, exp, value); 32         } catch (MemcachedException e) { 33             throw new MemcachedClientException(e); 34         } 35     } 36  37     @Override 38     public boolean set(String key, int exp, Object value, long timeout) throws TimeoutException, 39             InterruptedException, MemcachedClientException { 40         try { 41             return client.set(key, exp, value, timeout); 42         } catch (MemcachedException e) { 43             throw new MemcachedClientException(e); 44         } 45     } 46  47     @Override 48     public void setWithNoReply(String key, int exp, Object value) throws InterruptedException, 49             MemcachedClientException { 50         try { 51             client.setWithNoReply(key, exp, value); 52         } catch (MemcachedException e) { 53             throw new MemcachedClientException(e); 54         } 55     } 56  57     @Override 58     public boolean add(String key, int exp, Object value) throws TimeoutException, InterruptedException, 59             MemcachedClientException { 60         try { 61             return client.add(key, exp, value); 62         } catch (MemcachedException e) { 63             throw new MemcachedClientException(e); 64         } 65     } 66  67     @Override 68     public boolean add(String key, int exp, Object value, long timeout) throws TimeoutException, 69             InterruptedException, MemcachedClientException { 70         try { 71             return client.add(key, exp, value, timeout); 72         } catch (MemcachedException e) { 73             throw new MemcachedClientException(e); 74         } 75     } 76  77     @Override 78     public void addWithNoReply(String key, int exp, Object value) throws InterruptedException, 79             MemcachedClientException { 80         try { 81             client.addWithNoReply(key, exp, value); 82         } catch (MemcachedException e) { 83             throw new MemcachedClientException(e); 84         } 85     } 86  87     @Override 88     public boolean replace(String key, int exp, Object value) throws TimeoutException, InterruptedException, 89             MemcachedClientException { 90         try { 91             return client.replace(key, exp, value); 92         } catch (MemcachedException e) { 93             throw new MemcachedClientException(e); 94         } 95     } 96  97     @Override 98     public boolean replace(String key, int exp, Object value, long timeout) throws TimeoutException, 99             InterruptedException, MemcachedClientException {100         try {101             return client.replace(key, exp, value, timeout);102         } catch (MemcachedException e) {103             throw new MemcachedClientException(e);104         }105     }106 107     @Override108     public void replaceWithNoReply(String key, int exp, Object value) throws InterruptedException,109             MemcachedClientException {110         try {111             client.replaceWithNoReply(key, exp, value);112         } catch (MemcachedException e) {113             throw new MemcachedClientException(e);114         }115     }116 117     @Override118     public boolean append(String key, Object value) throws TimeoutException, InterruptedException,119             MemcachedClientException {120         try {121             return client.append(key, value);122         } catch (MemcachedException e) {123             throw new MemcachedClientException(e);124         }125     }126 127     @Override128     public boolean append(String key, Object value, long timeout) throws TimeoutException, InterruptedException,129             MemcachedClientException {130         try {131             return client.append(key, value, timeout);132         } catch (MemcachedException e) {133             throw new MemcachedClientException(e);134         }135     }136 137     @Override138     public void appendWithNoReply(String key, Object value) throws InterruptedException, MemcachedClientException {139         try {140             client.appendWithNoReply(key, value);141         } catch (MemcachedException e) {142             throw new MemcachedClientException(e);143         }144     }145 146     @Override147     public boolean prepend(String key, Object value) throws TimeoutException, InterruptedException,148             MemcachedClientException {149         try {150             return client.prepend(key, value);151         } catch (MemcachedException e) {152             throw new MemcachedClientException(e);153         }154     }155 156     @Override157     public boolean prepend(String key, Object value, long timeout) throws TimeoutException, InterruptedException,158             MemcachedClientException {159         try {160             return client.prepend(key, value, timeout);161         } catch (MemcachedException e) {162             throw new MemcachedClientException(e);163         }164     }165 166     @Override167     public void prependWithNoReply(String key, Object value) throws InterruptedException, MemcachedClientException {168         try {169             client.prependWithNoReply(key, value);170         } catch (MemcachedException e) {171             throw new MemcachedClientException(e);172         }173     }174 175     @Override176     public long incr(String key, long delta, long initValue) throws TimeoutException, InterruptedException,177             MemcachedClientException {178         try {179             return client.incr(key, delta, initValue);180         } catch (MemcachedException e) {181             throw new MemcachedClientException(e);182         }183     }184 185     @Override186     public long incr(String key, long delta, long initValue, long timeout) throws TimeoutException,187             InterruptedException, MemcachedClientException {188         try {189             return client.incr(key, delta, initValue, timeout);190         } catch (MemcachedException e) {191             throw new MemcachedClientException(e);192         }193     }194 195     @Override196     public void incrWithNoReply(String key, long delta) throws InterruptedException, MemcachedClientException {197         try {198             client.incrWithNoReply(key, delta);199         } catch (MemcachedException e) {200             throw new MemcachedClientException(e);201         }202     }203 204     @Override205     public long decr(String key, long delta, long initValue) throws TimeoutException, InterruptedException,206             MemcachedClientException {207         try {208             return client.decr(key, delta, initValue);209         } catch (MemcachedException e) {210             throw new MemcachedClientException(e);211         }212     }213 214     @Override215     public long decr(String key, long delta, long initValue, long timeout) throws TimeoutException,216             InterruptedException, MemcachedClientException {217         try {218             return client.decr(key, delta, initValue, timeout);219         } catch (MemcachedException e) {220             throw new MemcachedClientException(e);221         }222     }223 224     @Override225     public void decrWithNoReply(String key, long delta) throws InterruptedException, MemcachedClientException {226         try {227             client.decrWithNoReply(key, delta);228         } catch (MemcachedException e) {229             throw new MemcachedClientException(e);230         }231     }232 233     @Override234     public boolean delete(String key) throws TimeoutException, InterruptedException, MemcachedClientException {235         try {236             return client.delete(key);237         } catch (MemcachedException e) {238             throw new MemcachedClientException(e);239         }240     }241 242     @Override243     public boolean delete(String key, long opTimeout) throws TimeoutException, InterruptedException,244             MemcachedClientException {245         try {246             return client.delete(key, opTimeout);247         } catch (MemcachedException e) {248             throw new MemcachedClientException(e);249         }250     }251 252     @Override253     public boolean delete(String key, long cas, long opTimeout) throws TimeoutException, InterruptedException,254             MemcachedClientException {255         try {256             return client.delete(key, cas, opTimeout);257         } catch (MemcachedException e) {258             throw new MemcachedClientException(e);259         }260     }261 262     @Override263     public void deleteWithNoReply(String key) throws InterruptedException, MemcachedClientException {264         try {265             client.deleteWithNoReply(key);266         } catch (MemcachedException e) {267             throw new MemcachedClientException(e);268         }269     }270 271     @SuppressWarnings("unchecked")272     @Override273     public <T> T get(String key) throws TimeoutException, InterruptedException, MemcachedClientException {274         try {275             return (T)client.get(key);276         } catch (MemcachedException e) {277             throw new MemcachedClientException(e);278         }279     }280 281     @SuppressWarnings("unchecked")282     @Override283     public <T> T get(String key, long timeout) throws TimeoutException, InterruptedException,284             MemcachedClientException {285         try {286             return (T)client.get(key, timeout);287         } catch (MemcachedException e) {288             throw new MemcachedClientException(e);289         }290     }291 292     @Override293     public <T> Map<String, T> get(Collection<String> keyCollections) throws TimeoutException,294             InterruptedException, MemcachedClientException {295         try {296             return client.get(keyCollections);297         } catch (MemcachedException e) {298             throw new MemcachedClientException(e);299         }300     }301 302     @Override303     public <T> Map<String, T> get(Collection<String> keyCollections, long timeout) throws TimeoutException,304             InterruptedException, MemcachedClientException {305         try {306             return client.get(keyCollections, timeout);307         } catch (MemcachedException e) {308             throw new MemcachedClientException(e);309         }310     }311 312     @SuppressWarnings("unchecked")313     @Override314     public <T> GetsResponse<T> gets(String key) throws TimeoutException, InterruptedException,315             MemcachedClientException {316         try {317             return new GetsResponse<T>((net.rubyeye.xmemcached.GetsResponse<T>) client.gets(key));318         } catch (MemcachedException e) {319             throw new MemcachedClientException(e);320         }321     }322 323     @SuppressWarnings("unchecked")324     @Override325     public <T> GetsResponse<T> gets(String key, long timeout) throws TimeoutException, InterruptedException,326             MemcachedClientException {327         try {328             return new GetsResponse<T>((net.rubyeye.xmemcached.GetsResponse<T>) client.gets(key, timeout));329         } catch (MemcachedException e) {330             throw new MemcachedClientException(e);331         }332     }333 334     @Override335     public <T> Map<String, GetsResponse<T>> gets(Collection<String> keyCollections) throws TimeoutException,336             InterruptedException, MemcachedClientException {337         try {338             Map<String, GetsResponse<T>> results = new HashMap<String, GetsResponse<T>>();339             Map<String, net.rubyeye.xmemcached.GetsResponse<T>> tmps = client.gets(keyCollections);340             if (tmps != null) {341                 Set<String> keys = tmps.keySet();342                 for (String key : keys) {343                     results.put(key, new GetsResponse<T>((net.rubyeye.xmemcached.GetsResponse<T>) tmps.get(key)));344                 }345             }346             return results;347         } catch (MemcachedException e) {348             throw new MemcachedClientException(e);349         }350     }351 352     @Override353     public <T> Map<String, GetsResponse<T>> gets(Collection<String> keyCollections, long timeout)354             throws TimeoutException, InterruptedException, MemcachedClientException {355         try {356             Map<String, GetsResponse<T>> results = new HashMap<String, GetsResponse<T>>();357             Map<String, net.rubyeye.xmemcached.GetsResponse<T>> tmps = client.gets(keyCollections, timeout);358             if (tmps != null) {359                 Set<String> keys = tmps.keySet();360                 for (String key : keys) {361                     results.put(key, new GetsResponse<T>((net.rubyeye.xmemcached.GetsResponse<T>) tmps.get(key)));362                 }363             }364             return results;365         } catch (MemcachedException e) {366             throw new MemcachedClientException(e);367         }368     }369 370     @Override371     public boolean isEnabled() {372         return enabled;373     }374 375     public void setEnabled(boolean enabled) {376         this.enabled = enabled;377     }378 379 }

经验总结:

   缓存数据的一致性问题
   缓存数据尽量只读,因此缓存本身是不适合大量写和更新操作的数据场景的。对于读的情况下,如果存在数据变化,一种是同时更新缓存和数据库。一种是直接对缓存数据进行失效处理。敲命令行的时候换行的时候要跟空格.

 

另附:memcached的常用命令

参考:http://www.cnblogs.com/jeffwongishandsome/archive/2011/11/06/2238265.html