func (c *cacheRepo) WriteCacheIfEmpty(key string, data interface{}, ttl time.Duration) (err error) {
	c.kmutex.Lock(key)
	defer c.kmutex.Unlock(key)

	// check whether cache value is empty
	conn := c.opt.CachePool.Get()
	defer conn.Close()

	_, err = conn.Do("GET", key)
	if err != nil {
		if err == redis.ErrNil {
			return nil //return nil as the data already set, no need to overwrite
		}

		return err
	}

	// write data to cache
	_, err = conn.Do("SETEX", key, ttl.Seconds(), data)
	if err != nil {
		return err
	}

	return nil
}
// WriteCacheIfEmpty will try to write to cache, if the data still empty after locking
WriteCacheIfEmpty will try to write to cache, if the data still empty after locking
// WriteCacheIfEmpty will try to write to cache if the data still empty after locking
func (c *cacheRepo) WriteCacheIfEmpty(key string, data interface{}, ttl time.Duration) (err error) {
	c.kmutex.Lock(key)
	defer c.kmutex.Unlock(key)
	conn := c.opt.CachePool.Get()
	defer conn.Close()
	_, err = conn.Do("GET", key)
	if err != nil {
		if err == redis.ErrNil {
			return nil 
		}
		return err
	}
	_, err = conn.Do("SETEX", key, ttl.Seconds(), data)
	if err != nil {
		return err
	}
	return nil
}
func ( c * cacheRepo ) WriteCacheIfEmpty ( key string , data interface { } , ttl time . Duration ) ( err error ) { c . kmutex . Lock ( key ) defer c . kmutex . Unlock ( key ) conn := c . opt . CachePool . Get ( ) defer conn . Close ( ) _ , err = conn . Do ( " " , key ) if err != nil { if err == redis . ErrNil { return nil } return err } _ , err = conn . Do ( " " , key , ttl . Seconds ( ) , data ) if err != nil { return err } return nil }
WriteCacheIfEmpty will try to write to cache, if the data still empty after locking
WriteCacheIfEmpty will try to write to cache if the data still empty after locking
// check whether cache value is empty //return nil as the data already set, no need to overwrite // write data to cache