<?php

namespace tw\redis\traits;

use tw\redis\TwRedis;


trait StringTrait
{
    public function get($word)
    {
        return TwRedis::get($this->key($word));
    }

    public function set($word, $val)
    {
        return TwRedis::set($this->key($word), $val);
    }

    /**
     * 设置 string 同时设置 TTL
     * 
     * @param int|string $word: 关键字
     * @param int|float|string $val: 值
     * @param int $expire: TTL in millisecond  1s = 1000 millisecond
     */
    public function set_with_expire($word, $val, $expire)
    {
        return TwRedis::setEx($this->key($word), $expire, $val);
    }

    /**
     * key 不存在时设置
     */
    public function setnx($word, $val)
    {
        return TwRedis::setNx($this->key($word), $val);
    }

    public function incr_by($word, $step)
    {
        return TwRedis::incrBy($this->key($word), $step);
    }

    public function incr_by_float($word, $f)
    {
        return TwRedis::incrByFloat($this->key($word), $f);
    }

    public function decr_by($word, $step)
    {
        return TwRedis::decrBy($this->key($word), $step);
    }
}
