【发布时间】:2017-07-31 02:42:56
【问题描述】:
我在数据库中有一个项目列表,每个项目都可以选择否决或赞成。这些投票与其他项目字段一起存储在 MySql 中。比如这样的:
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 的连接。我怎样才能改进这一点,或者这是我不应该太担心的事情,因为缓存服务器可以毫无问题地处理这么多的连接。
【问题讨论】:
标签: php mysql laravel caching eloquent