ADD Storage

This commit is contained in:
Krivchikov Dmitry 2016-01-15 20:54:23 +03:00
parent 0dd54c9ae3
commit b3f5203d85
2 changed files with 47 additions and 1 deletions

View File

@ -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']);
}

40
models/Storage.php Normal file
View File

@ -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;
}
}