<?php

use Illuminate\Support\Facades\Redis;

// ...
public static function getByIdWithCache($id) {
Redis::select(6);
$key = sprintf("CarV2:MERCHANT:%d", $id);
$columns = array( "id", "merchant_type_id","member_username","company_full_name",
"city_code","recommend_id","name");
$values = Redis::hMget($key, $columns);
if (!empty($values[0])) {
$mch = new Merchants();
for ($i = 0, $n = count($values); $i < $n; $i++) {
$mch->setAttribute($columns[$i], $values[$i]);
}
return $mch;
}
/** @var $mch Merchants */
$mch = self::where("id", "=", $id)->get($columns)->first();
if (empty($mch)) {
Redis::hSet($key, "id", 0);
return $mch;
}
foreach ($columns as $c) {
Redis::hSet($key, $c, $mch->getAttribute($c));
}
Redis::expire($key, 3600);
return $mch;
}

?>