PageRenderTime 6934ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/src/com/jieshuhuiyou/util/memcachedHelper.java

https://bitbucket.org/psjay/ants-bookbase
Java | 110 lines | 54 code | 22 blank | 34 comment | 0 complexity | c0dc7277694f759b6b6c5c746edab18e MD5 | raw file
  1. package com.jieshuhuiyou.util;
  2. import java.util.Date;
  3. import com.alibaba.fastjson.annotation.JSONType;
  4. import com.danga.MemCached.MemCachedClient;
  5. import com.danga.MemCached.SockIOPool;
  6. /**
  7. * memcahed???
  8. * @author Michael
  9. *
  10. */
  11. public class memcachedHelper {
  12. // ?????????
  13. protected static MemCachedClient mcc = new MemCachedClient();
  14. protected static memcachedHelper memCached = new memcachedHelper();
  15. // ????????????
  16. static {
  17. // ?????????
  18. String[] servers = {"127.0.0.1:11211"};
  19. Integer[] weights = {3};
  20. // ??socke????????
  21. SockIOPool pool = SockIOPool.getInstance();
  22. // ???????
  23. pool.setServers( servers );
  24. pool.setWeights( weights );
  25. // ????????????????????????
  26. pool.setInitConn( 5 );
  27. pool.setMinConn( 5 );
  28. pool.setMaxConn( 250 );
  29. pool.setMaxIdle( 1000 * 60 * 60 * 6 );
  30. // ??????????
  31. pool.setMaintSleep( 30 );
  32. // ??TCP?????????
  33. pool.setNagle( false );
  34. pool.setSocketTO( 3000 );
  35. pool.setSocketConnectTO( 0 );
  36. // ??????
  37. pool.initialize();
  38. // ???????????????K?????????
  39. mcc.setCompressEnable( true );
  40. mcc.setCompressThreshold( 64 * 1024 );
  41. }
  42. /**
  43. * ???????????????
  44. *
  45. */
  46. protected memcachedHelper()
  47. {
  48. }
  49. /**
  50. * ??????.
  51. * @return
  52. */
  53. public static memcachedHelper getInstance()
  54. {
  55. return memCached;
  56. }
  57. /**
  58. * ????????????.
  59. * @param key
  60. * @param value
  61. * @return
  62. */
  63. public boolean add(String key, Object value)
  64. {
  65. return mcc.add(key, value);
  66. }
  67. public boolean add(String key, Object value, Date expiry)
  68. {
  69. return mcc.add(key, value, expiry);
  70. }
  71. public boolean replace(String key, Object value)
  72. {
  73. return mcc.replace(key, value);
  74. }
  75. public boolean replace(String key, Object value, Date expiry)
  76. {
  77. return mcc.replace(key, value, expiry);
  78. }
  79. /**
  80. * ????????????.
  81. * @param key
  82. * @return
  83. */
  84. public Object get(String key)
  85. {
  86. return mcc.get(key);
  87. }
  88. }