我需要根据用户的保留日给予一些奖励。例如:
2016-01-13 - [R0] day of my registration, I'll receive bonus x1
2016-01-15 - [R1] it's my first day of retention, the same for x2 bonus
2016-01-16 - [--] no visit
2016-01-17 - [R0] again bonus x1
2016-01-18 - [R1] again bonus x2
2016-01-19 - [R2] again bonus x3
2016-01-20 - [--] no bonus again
2016-01-21 - [R0] again bonus x1
我不想在sql表中存储用户活动,我有redis存储空间,可以在其中存储userId => anyValue。
我的想法就在附近:
$registrationTimestamp = $User->getRegistration(); # we have the user registration date
$registrationMidnight = midnight($registrationTimestamp); # take 00:00 of this day
$todayMidnight = midnight(time()); # today 00:00
$dayOfRetention = ($todayMidnight - $registrationMidnight) / 86400;
if ($dayOfRetention === 0) {
// user was registered today, do nothing
return;
}
$currentDayOfRetention = $User->getDayOfRetention(); # get current value from redis
if ($dayOfRetention - $currentDayOfRetention === 1) {
// user was on site yesterday
// GAVE HIM BONUS x $dayOfRetention
}
$User->setDayOfRetention($dayOfRetention); # and increment it anyway
但我不认为这是最好的解决方案。有什么想法吗?