<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/9/1
 * Time: 下午10:38
 */

namespace activity;


class version_checker
{
    private $cur_version;
    private $reset_time;
    private $interval;

    public function __construct($keyword,$interval = 60)
    {
        $this->keyword = $keyword;
        $this->cur_version = 0;
        $this->reset_time = 0;
        $this->interval = $interval;
    }

    public function need_init()
    {
        if($this->reset_time == 0) {
            $this->reset_time = time();
            return true;
        }
        else if((time() - $this->reset_time) < $this->interval) {
            return false;
        }
        else
        {
            $need_reset = false;
            $version = rkcache($this->keyword);
            if(empty($version)) {
                $this->cur_version = 1;
                wkcache($this->keyword, $this->cur_version);
                $need_reset = true;
                $this->reset_time = time();
            }
            else
            {
                $version = intval($version);
                if($version > $this->cur_version) {
                    $need_reset = true;
                    $this->cur_version = $version;
                }

                $this->reset_time = time();
            }

            return $need_reset;
        }
    }
}