0

I want to make async method in redis by StackExchange.Redis follow code :

public bool Insert<T>(T entity) where T : IBaseEntity
{
    long entityCounter = _redisClient.StringIncrement(CacheProcessPatterns.MakeItemCounter(entity.GetType().Name));

    if (entity.Id == 0)
    {
        entity.Id = ((int)GetLastId<T>()) + 1;
    }
    _redisClient.StringSet(CacheProcessPatterns.MakeLastId(entity.GetType().Name), entity.Id);
    string itemRedisKey = CacheProcessPatterns.MakeItemById(entity.GetType().Name, entity.Id);
    bool setStatus = _redisClient.StringSet(itemRedisKey, JsonSerializer.SerializeToString<T>(entity));
    if (setStatus)
    {
        _redisClient.StringSet(CacheProcessPatterns.MakeIdByKey(entity.GetType().Name, entity.Id), entity.Key.ToString());
        _redisClient.StringSet(CacheProcessPatterns.MakeKeyById(entity.GetType().Name, entity.Key.ToString()), entity.Id);
        _redisClient.SetAdd(CacheProcessPatterns.MakeItemKeysByType(entity.GetType().Name), entity.Id);
    }
    else
    {
        entityCounter = _redisClient.StringDecrement(CacheProcessPatterns.MakeItemCounter(entity.GetType().Name));
    }
    return setStatus;

}

and the other hands i trying make async but I have a problem on second ContinueWith() Method .

Error : Cannot implicity convert type 'Task' to 'Task'.An explicit conversation exists(are you missing a cast?).

Follow code :

public Task<bool> Insert<T>(T entity) where T : IBaseEntity
{
   return _redisClient.StringIncrementAsync(CacheProcessPatterns.MakeItemCounter(entity.GetType().Name))
        .ContinueWith(entityCounter =>
        {

            if (entity.Id == 0)
            {
                entity.Id = ((int)GetLastId<T>().Result);
            }

        }).ContinueWith(_ =>
        {


            _redisClient.StringSetAsync(CacheProcessPatterns.MakeLastId(entity.GetType().Name), entity.Id).ContinueWith(status =>
            {

                string itemRedisKey = CacheProcessPatterns.MakeItemById(entity.GetType().Name, entity.Id);
              _redisClient.StringSetAsync(itemRedisKey, JsonSerializer.SerializeToString<T>(entity)).ContinueWith( setStatus => 
              {
                    if (setStatus.Result)
                    {
                        ITransaction tran = _redisClient.CreateTransaction();
                        tran.StringSetAsync(CacheProcessPatterns.MakeIdByKey(entity.GetType().Name, entity.Id), entity.Key.ToString());
                        tran.StringSetAsync(CacheProcessPatterns.MakeKeyById(entity.GetType().Name, entity.Key.ToString()), entity.Id);
                        tran.SetAddAsync(CacheProcessPatterns.MakeItemKeysByType(entity.GetType().Name), entity.Id);
                        return tran.ExecuteAsync();
                    }
                    else
                    {
                        _redisClient.StringDecrementAsync(CacheProcessPatterns.MakeItemCounter(entity.GetType().Name));
                    }

                    return setStatus;

              });

            });
        });
}

What is my problem ? and how to fix that ?Thanks ...

Amir Movahedi
  • 1,802
  • 3
  • 29
  • 52

1 Answers1

0

I think the problem is that your second ContinueWith returns a Task and not a Task<bool>. Try changing the code as follows:

public Task<bool> Insert<T>(T entity) where T : IBaseEntity
{
   return _redisClient.StringIncrementAsync(CacheProcessPatterns.MakeItemCounter(entity.GetType().Name))
        .ContinueWith(entityCounter =>
        {

            if (entity.Id == 0)
            {
                entity.Id = ((int)GetLastId<T>().Result);
            }

        })
        // Explicitly specify task type to be bool
        .ContinueWith<bool>(_ =>
        {
            _redisClient.StringSetAsync(CacheProcessPatterns.MakeLastId(entity.GetType().Name), entity.Id).ContinueWith(status =>
            {

                string itemRedisKey = CacheProcessPatterns.MakeItemById(entity.GetType().Name, entity.Id);
              _redisClient.StringSetAsync(itemRedisKey, JsonSerializer.SerializeToString<T>(entity)).ContinueWith( setStatus =>
              {
                    if (setStatus.Result)
                    {
                        ITransaction tran = _redisClient.CreateTransaction();
                        tran.StringSetAsync(CacheProcessPatterns.MakeIdByKey(entity.GetType().Name, entity.Id), entity.Key.ToString());
                        tran.StringSetAsync(CacheProcessPatterns.MakeKeyById(entity.GetType().Name, entity.Key.ToString()), entity.Id);
                        tran.SetAddAsync(CacheProcessPatterns.MakeItemKeysByType(entity.GetType().Name), entity.Id);
                        return tran.ExecuteAsync();
                    }
                    else
                    {
                        _redisClient.StringDecrementAsync(CacheProcessPatterns.MakeItemCounter(entity.GetType().Name));
                    }

                    return setStatus;

              });

            });

            return true;  // since this is a Task<bool> we need a bool return value
NeddySpaghetti
  • 13,187
  • 5
  • 32
  • 61