memcache.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 net.sf.l2j.gameserver.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. private static final memcache _instance = new memcache();
  26. public static memcache getInstance()
  27. {
  28. return _instance;
  29. }
  30. private memcache()
  31. {
  32. _hms = new HashMap<Integer,String>();
  33. _hmi = new HashMap<Integer,Integer>();
  34. _lastAccess = new HashMap<Integer,Long>();
  35. }
  36. private void checkExpired()
  37. {
  38. for(Integer k : _hmi.keySet())
  39. if(_lastAccess.get(k) + 3600000 < System.currentTimeMillis())
  40. {
  41. // _hmi.remove(k);
  42. // _last_access.remove(k);
  43. }
  44. for(Integer k : _hms.keySet())
  45. if(_lastAccess.get(k) + 3600000 < System.currentTimeMillis())
  46. {
  47. // _hms.remove(k);
  48. // _last_access.remove(k);
  49. }
  50. }
  51. public void set(String type, String key, int value)
  52. {
  53. int hash = (type+"->"+key).hashCode();
  54. // _log.fine("Set memcache "+type+"("+key+")["+hash+"] to "+value);
  55. _hmi.put(hash, value);
  56. _lastAccess.put(hash, System.currentTimeMillis());
  57. checkExpired();
  58. }
  59. @Deprecated
  60. public boolean isSet(String type, String key)
  61. {
  62. int hash = (type+"->"+key).hashCode();
  63. boolean exists = _hmi.containsKey(hash) || _hms.containsKey(hash);
  64. if(exists)
  65. _lastAccess.put(hash, System.currentTimeMillis());
  66. checkExpired();
  67. _log.fine("Check exists memcache "+type+"("+key+")["+hash+"] is "+exists);
  68. return exists;
  69. }
  70. @Deprecated
  71. public Integer getInt(String type, String key)
  72. {
  73. int hash = (type+"->"+key).hashCode();
  74. _lastAccess.put(hash, System.currentTimeMillis());
  75. checkExpired();
  76. _log.fine("Get memcache "+type+"("+key+")["+hash+"] = "+_hmi.get(hash));
  77. return _hmi.get(hash);
  78. }
  79. }