ADD Storage
This commit is contained in:
parent
0dd54c9ae3
commit
b3f5203d85
|
|
@ -66,7 +66,13 @@ class IndexController extends Controller
|
|||
self::addVar('userLat', $_COOKIE['userLat']);
|
||||
self::addVar('userLng', $_COOKIE['userLng']);
|
||||
} else {
|
||||
$loc = IpInfo::geo(isset($_SERVER['HTTP_X_REAL_IP']) ? $_SERVER['HTTP_X_REAL_IP'] : $_SERVER['REMOTE_ADDR']);
|
||||
$ip = isset($_SERVER['HTTP_X_REAL_IP']) ? $_SERVER['HTTP_X_REAL_IP'] : $_SERVER['REMOTE_ADDR'];
|
||||
// $ip = '91.204.150.217';
|
||||
$loc = Storage::get($ip);
|
||||
if(!$loc) {
|
||||
$loc = IpInfo::geo($ip);
|
||||
Storage::model()->set($ip, $loc, 604800); // ttl = week
|
||||
}
|
||||
self::addVar('userLat', $loc['lat']);
|
||||
self::addVar('userLng', $loc['lng']);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
class Storage extends Model
|
||||
{
|
||||
|
||||
function __construct($fromArray = array())
|
||||
{
|
||||
$this->_tableName_ = 'storage';
|
||||
parent::__construct($fromArray);
|
||||
}
|
||||
|
||||
static function model()
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
static function get($key)
|
||||
{
|
||||
$val = self::model()->getAll(array('where' => '(`key` = "'. md5($key).'") AND (`ttl` >= '.time().')', 'limit' => 1));
|
||||
return $val ? unserialize($val->value) : NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param type $key
|
||||
* @param type $value
|
||||
* @param int $ttl seconds
|
||||
* @return boolean
|
||||
*/
|
||||
static function set($key, $value, $ttl = 60)
|
||||
{
|
||||
$value = serialize($value);
|
||||
$ttl = time()+$ttl;
|
||||
|
||||
$request = App::DB()->prepare("INSERT INTO `storage` SET `key`=?, `keyRaw` = ?, `value`=?, `ttl`=? ON DUPLICATE KEY UPDATE `value`=?, `ttl`=? ;");
|
||||
$res = $request->execute(array(md5($key), $key, $value, $ttl, $value, $ttl));
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue