Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于set浮点数失误的问题 #1

Open
lovecn opened this issue Oct 17, 2018 · 0 comments
Open

关于set浮点数失误的问题 #1

lovecn opened this issue Oct 17, 2018 · 0 comments

Comments

@lovecn
Copy link

lovecn commented Oct 17, 2018

关于set方法,我测试了下$redis->set('test',13.6);取出来正常的?redis版本3.0.7

 /**
     * 存储一个键值
     * @param  string or int $key 键名
     * @param  mix $value 键值,支持数组、对象
     * @param  int $expire 过期时间(秒)
     * @return bool 返回布尔值
     */
    public static function set($key, $value, $expire='')
    {
        if(is_int($key) || is_string($key)){
            //如果是int类型的数字就不要序列化,否则用自增自减功能会失败,
            //如果不序列化,set()方法只能保存字符串和数字类型,
            //如果不序列化,浮点型数字会有失误,如13.6保存,获取时是13.59999999999
            $value = is_int($value) ? $value : serialize($value);
            $expire = (int)$expire ? $expire : self::$expire;
            if(self::$redis->set($key, $value) && self::$redis->expire($key, $expire)){
                return true;
            }
            return false;
        }
        return false;
    }

还有 sadd支持数组,不用循环sadd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant