异步Lazy <t>立即获得结果

时间:2018-08-01 07:53:35

标签: c# asp.net .net lazy-initialization

我有以下内容:

public class User 
{
        private readonly Lazy<Task<List<ReminderDb>>> _reminders;

        public SmsUserDb()
        {
            // Get _reminderReader from IoC

            _reminders = new Lazy<Task<List<ReminderDb>>>(async () => (List<ReminderDb>)await _reminderReader.Get(UserId));
        }

        public string UserId { get; set; }
        public Task<List<ReminderDb>> Reminders => _reminders.Value;
}

当我实例化一个对象时,像这样:

    var n = new SmsUserDb {UserId = "123456"};
    var rems = await n.Reminders;

此代码有效,我看到n.Reminders =“等待激活”,直到我点击等待n.Reminders行。

但是,当我像这样从缓存中查询此User对象时:

        var n1 = await _userReader.GetUserFromCache("+17084556675"); // return SmsUserDb
        var n2 = await n1.Reminders;

当它命中GetUserFromCache()时,立即调用_reminderReader.Get(UserId),后者再次调用缓存以获取提醒。然后它只是超时。因此,懒惰无法正常工作,很可能导致死锁。

public async Task<SmsUserDb> GetUserFromCache(string phoneNumber)
{
    var hash = CachedObjectType.smsuser.ToString();
    var fieldKey = string.Format($"{CachedObjectType.smsuser.ToString()}:user-{phoneNumber}");
    var result = await _cacheUserService.GetHashedAsync(hash, fieldKey);
    return result;
}

    private async Task<List<ReminderDb>> GetRemindersFromCache(string userId)
    {
        var hash = CachedObjectType.smsreminder.ToString();
        var fieldKey = string.Format($"{CachedObjectType.smsreminder.ToString()}:user-{userId}");
        var result = await _cacheService.GetHashedAsync(hash, fieldKey);
        return result ?? new List<ReminderDb>();
    }

可能是什么问题?

2 个答案:

答案 0 :(得分:1)

在我的测试代码中一切正常(如下所示)。因此,错误必须出在您未显示给我们的某些代码中。

这里有一些有效的示例代码-证明Lazy<T>正常工作。因此,该错误位于代码的其他位置。

您必须发布可编译的副本,以便任何人帮助您。

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Demo
{
    public class ReminderDb
    {
        public string Value;
    }

    public class ReminderReader
    {
        public async Task<List<ReminderDb>> Get(string userId)
        {
            Console.WriteLine("In Get() for " + userId);
            await Task.Delay(1000);
            Console.WriteLine("Returning from Get()");
            return new List<ReminderDb>{new ReminderDb{Value = userId}};
        }
    }

    public class Cache
    {
        public void Add(string key, SmsUserDb value)
        {
            _cache.Add(key, value);
        }

        public async Task<SmsUserDb> GetHashedAsync(string key)
        {
            await Task.Delay(1000);
            return _cache[key];
        }

        readonly Dictionary<string, SmsUserDb> _cache = new Dictionary<string, SmsUserDb>();
    }

    public class SmsUserDb
    {
        readonly Lazy<Task<List<ReminderDb>>> _reminders;
        readonly ReminderReader _reminderReader = new ReminderReader();

        public SmsUserDb()
        {
            _reminders = new Lazy<Task<List<ReminderDb>>>(async () => (List<ReminderDb>) await _reminderReader.Get(UserId));
        }

        public string                 UserId    { get; set; }
        public Task<List<ReminderDb>> Reminders => _reminders.Value;
    }

    static class Program
    {
        static async Task Main()
        {
            var db = new SmsUserDb(){UserId = "user ID"};
            var cache = new Cache();
            cache.Add("key", db);

            Console.WriteLine("Press RETURN to await cache.GetHashedAsync()");
            Console.ReadLine();
            var result = await cache.GetHashedAsync("key");

            Console.WriteLine("Press RETURN to await Reminders[0].Value");
            Console.ReadLine();
            Console.WriteLine((await result.Reminders)[0].Value);
        }
    }
}

答案 1 :(得分:0)

发现了问题。我正在使用相同的类来写入缓存,即:

public class User 
{
        private readonly Lazy<Task<List<ReminderDb>>> _reminders;

        public SmsUserDb()
        {
            // Get _reminderReader from IoC

            _reminders = new Lazy<Task<List<ReminderDb>>>(async () => (List<ReminderDb>)await _reminderReader.Get(UserId));
        }

        public string UserId { get; set; }
        public Task<List<ReminderDb>> Reminders => _reminders.Value;
}

在使用不同的类进行写作(在下面)和阅读(在上面)之后,所有工作都按需进行。谢谢

public class CacheUser {
    public string UserId { get; set; }
    public List<ReminderDb> Reminders {get; set; }
}