CacheMg.class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /*
  3. * Manage data in cache
  4. *
  5. * author: xuchangzhang
  6. *
  7. * */
  8. require_once(ONU_ROOT . 'application/lib/data/Base.class.php');
  9. class CacheMg{
  10. public static $Key_PromoteUserCnt = 'CACHE_KEY_PROMOTE_USER_CNT';
  11. public static $Key_UidIpPromCntLimit = 'CACHE_KEY_UID_IP_PROMOTE_CNT_LIMIT';
  12. private static $memcache;
  13. private static $db;
  14. private static function getCacheIns(){
  15. if(!isset(self::$memcache)){
  16. global $_ONU_CONFIG;
  17. self::$memcache = new Mcache($_ONU_CONFIG['_memcache_conf']);
  18. }
  19. return self::$memcache;
  20. }
  21. private function getDbIns(){
  22. if(!isset(self::$db)){
  23. global $_ONU_CONFIG;
  24. self::$db = new MysqlHandler($_ONU_CONFIG['_db_conf']);
  25. }
  26. return self::$db;
  27. }
  28. public static function get($key){
  29. $cache = self::getCacheIns();
  30. return $cache->get($key);
  31. }
  32. public static function set($key,$val){
  33. $cache = self::getCacheIns();
  34. return $cache->set($key,$val);
  35. }
  36. public static function getPromoteUserCnt($uid){
  37. $cache = self::getCacheIns();
  38. $userCntSet = $cache->get(self::$Key_PromoteUserCnt);
  39. if(empty($userCntSet)){
  40. $userCntSet = array();
  41. }
  42. else{
  43. $userCntSet = json_decode($userCntSet,true);
  44. }
  45. $uNumber = $userCntSet[$uid];
  46. // no data in cache get from db
  47. if(empty($uNumber)){
  48. $res = self::getPromoteUserCntDB($uid);
  49. $uNumber = (is_array($res))? $res[0]['cnt'] : 0;
  50. // update data to cache
  51. $userCntSet[$uid] = $uNumber;
  52. $cache->set(self::$Key_PromoteUserCnt, json_encode($userCntSet));
  53. }
  54. return $uNumber;
  55. }
  56. private static function getPromoteUserCntDB($uid){
  57. $db = self::getDbIns();
  58. $effectiveLevel = EFFECTIVE_PLAYER_LEVEL;
  59. $sql = "select count(id) cnt from onu_promote where uid='$uid' and player_grade =>$effectiveLevel;";
  60. return $db->query($sql);
  61. }
  62. // set method must be called after get method
  63. public static function setPromoteUserCnt($uid,$number){
  64. $cache = self::getCacheIns();
  65. $userCntSet = $cache->get(self::$Key_PromoteUserCnt);
  66. $userCntSet = json_decode($userCntSet,true);
  67. if(is_array($userCntSet)){
  68. $userCntSet[$uid] = $number;
  69. $cache->set(self::$Key_PromoteUserCnt, json_encode($userCntSet));
  70. }
  71. }
  72. //
  73. private static function getDayTimeKey_BJ(){
  74. date_default_timezone_set("Asia/Chongqing");
  75. $today = date("Ymd");
  76. return strtotime($today);
  77. }
  78. //
  79. // one ip can registe at most 5 users for one promote user in 3 days
  80. //
  81. public static function setUidIpPrmoteCnt_Plus($uid,$ip,$num){
  82. $cache = self::getCacheIns();
  83. $timeKey = self::getDayTimeKey_BJ();
  84. $itemKey = "$uid-$ip";
  85. $dataKey = '';
  86. $dataSet = $cache->get(self::$Key_UidIpPromCntLimit);
  87. $dataSet = json_decode($dataSet, true);
  88. if(empty($dataSet)) $dataSet = array();
  89. // set time period key
  90. if(count($dataSet) == 0){
  91. $dataSet[$timeKey] = array();
  92. $dataKey = $timeKey;
  93. }
  94. // set new key and clear the first key
  95. $oldPeriodKey = NULL;
  96. foreach($dataSet as $key=>$val){
  97. // over 3 days
  98. if( ($timeKey - $key) >= 259200){
  99. $oldPeriodKey = $key;
  100. }
  101. else{
  102. $dataKey = $key;
  103. }
  104. break;
  105. }
  106. if(!empty($oldPeriodKey)){
  107. $dataSet[$timeKey] = array();
  108. $dataKey = $timeKey;
  109. unset($dataSet[$oldPeriodKey]);
  110. }
  111. // set item value
  112. if(empty($dataSet[$dataKey][$itemKey])){
  113. $dataSet[$dataKey][$itemKey] = $num;
  114. }
  115. else{
  116. $dataSet[$dataKey][$itemKey] ++;
  117. }
  118. // update cache
  119. $cache->set(self::$Key_UidIpPromCntLimit,json_encode($dataSet));
  120. }
  121. public static function getUidIpPrmoteCnt($uid,$ip){
  122. $cache = self::getCacheIns();
  123. $itemKey = "$uid-$ip";
  124. $dataKey = '';
  125. $dataSet = $cache->get(self::$Key_UidIpPromCntLimit);
  126. $dataSet = json_decode($dataSet, true);
  127. if(empty($dataSet)) return 0;
  128. foreach($dataSet as $key=>$val){
  129. $dataKey = $key;
  130. }
  131. return $dataSet[$dataKey][$itemKey];
  132. }
  133. //Class end
  134. }