GuavaMemCached.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package com.primeton.damp.cache;
  2. import com.google.common.cache.Cache;
  3. import com.google.common.cache.CacheBuilder;
  4. /**
  5. * <pre>
  6. *
  7. * Created by zhenqin.
  8. * User: zhenqin
  9. * Date: 2021/4/2
  10. * Time: 11:15
  11. * Vendor: yiidata.com
  12. * To change this template use File | Settings | File Templates.
  13. *
  14. * </pre>
  15. *
  16. * @author zhenqin
  17. */
  18. public class GuavaMemCached implements ICache<Object> {
  19. /**
  20. * 缓存实现
  21. */
  22. final Cache<String, Object> cached = CacheBuilder.newBuilder().maximumSize(10000).initialCapacity(500).build();
  23. public GuavaMemCached() {
  24. }
  25. @Override
  26. public void add(String key, Object value) {
  27. cached.put(key, value);
  28. }
  29. @Override
  30. public void add(String key, int exp, Object value) {
  31. cached.put(key, value);
  32. }
  33. @Override
  34. public Object get(String key) {
  35. return cached.getIfPresent(key);
  36. }
  37. @Override
  38. public Object remove(String key) {
  39. cached.invalidate(key);
  40. return null;
  41. }
  42. @Override
  43. public int removeByPrefix(String prefix) {
  44. return 0;
  45. }
  46. @Override
  47. public void clear() {
  48. cached.cleanUp();
  49. }
  50. @Override
  51. public int size() {
  52. return (int)cached.size();
  53. }
  54. @Override
  55. public void shutdown() {
  56. clear();
  57. }
  58. }