/** * 所有的查询的结果缓存30天 */ public function __construct() { parent::__construct(new Application_Model_Table_City()); //设置缓存一个月 try { $this->_select = $this->select()->setCacheClass(XF_Cache_Memcache::getInstance())->setCacheTime(60 * 24 * 30, FALSE); } catch (XF_Exception $e) { } }
/** * 验证指定的验证码是否有效 * @param string $smsCode 手机验证码 * @return bool */ public function smsCodeIsOk($mobile, $smsCode) { try { $mem = XF_Cache_Memcache::getInstance(); } catch (XF_Exception $e) { return false; } $code = $mem->read($mobile . '_' . $smsCode); return $code == $smsCode; }
public function __construct() { parent::__construct($this); try { $this->setCache(XF_Cache_Memcache::getInstance()); } catch (XF_Exception $e) { } parent::__construct($this); $this->_view->setResourcePath($this->static_url); }
/** * 获取当前缓存对象实例 * @access public * @return XF_Cache_Memcache */ public static function getInstance() { if (self::$_instance == null) { //是否存在memcache缓存服务器配置 $servers = XF_Config::getInstance()->getMemcaches(); if (!isset($servers[0])) { throw new XF_Cache_Exception('Memcache server config is empty'); } $server = explode(':', $servers[0]); //检测是否安装memcache PHP扩展 if (!class_exists('Memcache', FALSE)) { throw new XF_Cache_Exception('Memcache PECL expands has not installed or has begun using'); } self::$_instance = new self(); self::$_instance->_memcache = new Memcache(); $status = self::$_instance->_memcache->connect($server[0], $server[1]); if ($status == FALSE) { throw new XF_Cache_Exception('Memcache connect failure'); } } return self::$_instance; }
/** * 设置数据缓存 * @access protected * @param mixed $data */ protected function _setDataCache($data, $saveFile) { if ($this->_data_cache_time > 0) { $mem = XF_Cache_Memcache::getInstance(); $mem->setCacheTime($this->_data_cache_time); $saveFile = md5($saveFile); $mem->add($saveFile, $data); } return true; }
/** * 发送验证码 * @param string $mobile 手机号码 * @param int $smsCode 验证码 * @throws XF_Exception */ public function sendSmsCode($mobile, $smsCode) { if (XF_String_Validate_Mobile::validate($mobile) == FALSE) { throw new XF_Exception('手机号码错误!'); } //获取模板 $event = $this->get(100); if ($event->status == '0') { return FALSE; } //短信 if ($event->status == '2') { if ($event->msm_tpl == '') { throw new XF_Exception('短信模板为空,无法发送!'); } $message = str_replace(array('{smsCode}', '{mobile}', '{hour}', '{minute}'), array($smsCode, $mobile, date('H'), date('i')), $event->msm_tpl); $status = $this->sendSms($mobile, $message, 'code'); if ($status == TRUE) { $mem = XF_Cache_Memcache::getInstance(); $mem->setCacheTime(10); $mem->add($mobile . '_' . $smsCode, $smsCode); } return $status; } return FALSE; }