using OASystem.RedisRepository.CommonHelper;
using OASystem.RedisRepository.Config;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OASystem.RedisRepository.RedisAsyncHelper
{/// <summary>
 /// Redis异步操作类  String部分类
 /// </summary>
    internal partial class RedisHelper : IRedisHelper
    {

        private static IDatabase _client;

        internal RedisHelper()
        {
            _client = RedisConnection.GenerateConnection.GetDatabase();
        }

        #region String 写操作

        /// <summary>
        ///  将任何数据添加到redis中
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public async Task<bool> StringSetAsync<T>(string key, T value, TimeSpan? timeout = null)
        {
            return await _client.StringSetAsync(key, SerializeHelper.Serialize(value), timeout);
        }

        public async Task<long> StringDecrementAsync(string key, long value = 1L)
        {
            return await _client.StringDecrementAsync(key, value);
        }

        public async Task<long> StringIncrementAsync(string key, long value = 1L)
        {
            return await _client.StringIncrementAsync(key, value);
        }
        #endregion

        #region String 读操作

        /// <summary>
        /// 根据key获取指定类型数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task<T> StringGetAsync<T>(string key)
        {
            return SerializeHelper.Deserialize<T>(await _client.StringGetAsync(key, CommandFlags.PreferSlave));
        }

        #endregion

    }
}
