Memcached.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /** Copyright 2009 by primeton Corporation.
  2. *
  3. * All rights reserved.
  4. *
  5. * This software is the confidential and proprietary information of
  6. * primeton Corporation ('Confidential Information'). You
  7. * shall not disclose such Confidential Information and shall use
  8. * it only in accordance with the terms of the license agreement
  9. * you entered into with primeton.
  10. */
  11. package com.primeton.dgs.kernel.core.cache.impl;
  12. import net.rubyeye.xmemcached.MemcachedClient;
  13. import com.primeton.dgs.kernel.core.cache.ICache;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import java.io.IOException;
  17. import java.io.Serializable;
  18. /**
  19. * @author xiongbin
  20. * @Version 2012-8-28
  21. */
  22. public class Memcached implements ICache<Serializable> {
  23. private static Logger logger = LoggerFactory.getLogger(Memcached.class);
  24. private MemcachedClient memcachedClient;
  25. public Memcached() {
  26. }
  27. public Memcached(MemcachedClient memcachedClient) {
  28. this.memcachedClient = memcachedClient;
  29. }
  30. @Override
  31. public void add(String key, Serializable value) {
  32. try {
  33. memcachedClient.add(key, 0, value);
  34. } catch (Exception e) {
  35. throw new IllegalStateException(e);
  36. }
  37. }
  38. @Override
  39. public void replace(String key, Serializable value) {
  40. try {
  41. memcachedClient.replace(key, 0, value);
  42. } catch (Exception e) {
  43. throw new IllegalStateException(e);
  44. }
  45. }
  46. @Override
  47. public Serializable get(String key) {
  48. try {
  49. return memcachedClient.get(key);
  50. } catch (Exception e) {
  51. throw new IllegalStateException(e);
  52. }
  53. }
  54. /**
  55. * 移除某条缓存
  56. * @param key 缓存的 KEY
  57. * @return
  58. */
  59. @Override
  60. public Serializable remove(String key) {
  61. try {
  62. Serializable obj = memcachedClient.get(key);
  63. memcachedClient.delete(key);
  64. return obj;
  65. } catch (Exception e) {
  66. throw new IllegalStateException(e);
  67. }
  68. }
  69. @Override
  70. public int removeByPrefix(String prefix) {
  71. logger.warn("can not support removeByPrefix.");
  72. return 0;
  73. }
  74. @Override
  75. public void clear() {
  76. try {
  77. memcachedClient.flushAll();
  78. } catch (Exception e) {
  79. throw new IllegalStateException(e);
  80. }
  81. }
  82. @Override
  83. public void add(String key, int exp, Serializable value) {
  84. try {
  85. memcachedClient.add(key, exp, value);
  86. } catch (Exception e) {
  87. throw new IllegalStateException(e);
  88. }
  89. }
  90. @Override
  91. public void replace(String key, int exp, Serializable value) {
  92. try {
  93. memcachedClient.replace(key, exp, value);
  94. } catch (Exception e) {
  95. throw new IllegalStateException(e);
  96. }
  97. }
  98. /**
  99. * 获得缓存队列的长度
  100. * @return 返回大于等于 0 则代表真实长度,返回 -1 代表无法获得
  101. */
  102. @Override public int size() {
  103. // memcache 无法获取当前长度
  104. return -1;
  105. }
  106. @Override
  107. public void close() throws IOException {
  108. memcachedClient.shutdown();
  109. }
  110. public MemcachedClient getMemcachedClient() {
  111. return memcachedClient;
  112. }
  113. public void setMemcachedClient(MemcachedClient memcachedClient) {
  114. this.memcachedClient = memcachedClient;
  115. }
  116. }