memcache.java 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.util.lib;
  16. import java.util.HashMap;
  17. import java.util.logging.Logger;
  18. @Deprecated
  19. public class memcache
  20. {
  21. private static Logger _log = Logger.getLogger(memcache.class.getName());
  22. private HashMap<Integer, String> _hms;
  23. private HashMap<Integer, Integer> _hmi;
  24. private HashMap<Integer, Long> _lastAccess;
  25. public static memcache getInstance()
  26. {
  27. return SingletonHolder._instance;
  28. }
  29. private memcache()
  30. {
  31. _hms = new HashMap<Integer, String>();
  32. _hmi = new HashMap<Integer, Integer>();
  33. _lastAccess = new HashMap<Integer, Long>();
  34. }
  35. private void checkExpired()
  36. {
  37. for (Integer k : _hmi.keySet())
  38. if (_lastAccess.get(k) + 3600000 < System.currentTimeMillis())
  39. {
  40. // _hmi.remove(k);
  41. // _last_access.remove(k);
  42. }
  43. for (Integer k : _hms.keySet())
  44. if (_lastAccess.get(k) + 3600000 < System.currentTimeMillis())
  45. {
  46. // _hms.remove(k);
  47. // _last_access.remove(k);
  48. }
  49. }
  50. public void set(String type, String key, int value)
  51. {
  52. int hash = (type + "->" + key).hashCode();
  53. //_log.fine("Set memcache "+type+"("+key+")["+hash+"] to "+value);
  54. _hmi.put(hash, value);
  55. _lastAccess.put(hash, System.currentTimeMillis());
  56. checkExpired();
  57. }
  58. @Deprecated
  59. public boolean isSet(String type, String key)
  60. {
  61. int hash = (type + "->" + key).hashCode();
  62. boolean exists = _hmi.containsKey(hash) || _hms.containsKey(hash);
  63. if (exists)
  64. _lastAccess.put(hash, System.currentTimeMillis());
  65. checkExpired();
  66. _log.fine("Check exists memcache " + type + "(" + key + ")[" + hash + "] is " + exists);
  67. return exists;
  68. }
  69. @Deprecated
  70. public Integer getInt(String type, String key)
  71. {
  72. int hash = (type + "->" + key).hashCode();
  73. _lastAccess.put(hash, System.currentTimeMillis());
  74. checkExpired();
  75. _log.fine("Get memcache " + type + "(" + key + ")[" + hash + "] = " + _hmi.get(hash));
  76. return _hmi.get(hash);
  77. }
  78. @SuppressWarnings("synthetic-access")
  79. private static class SingletonHolder
  80. {
  81. protected static final memcache _instance = new memcache();
  82. }
  83. }