我有一個數據庫中的項目列表,每個項目都有選項被選中。這些選票與其他項目字段一起存儲在MySql中。例如這樣的東西:從緩存中提取Laravel模型訪問器 - 性能增強
Schema::create('items', function ($table) {
$table->increments('id');
$table->text('message');
$table->integer('up_votes')->unsigned()->default(0);
$table->integer('down_votes')->unsigned()->default(0);
$table->timestamps();
});
用戶可以每天投票下/上。當用戶決定投票時,我將他的決定存儲到memcached一天,並相應地增加一個字段(up_votes或down_votes)。
$voteKey = sprintf('%s-%s', $request->ip(), $item->id);
if (!Cache::has($voteKey)) {
$vote = $request->get('vote');
$this->item->increment($vote ? 'up_votes' : 'down_votes');
Cache::put($voteKey, $vote, (60*24));
}
接下來我想要了解某些用戶投票的信息。我在模型中創建訪問:
public function getVoteAttribute($value)
{
$voteKey = sprintf('%s-%s', Request::ip(), $this->id);
return $this->attributes['vote'] = Cache::get($voteKey);
}
protected $appends = ['vote'];
是這種智能或莫不是長列出了一些性能問題?如果返回100個項目,則每個用戶有100個到memcached的連接。我該如何改善這一點,或者這是我不應該擔心的事情,因爲緩存服務器可以毫無問題地處理這種連接數量。