Example #1
0
    /// <summary>
    /// Finds the user with the specified emali address.
    /// </summary>
    /// <param name="email">The emali address for the user.</param>
    /// <returns>The <see cref="User"/>, if one was found, or null if the user was not found.</returns>
    public static async Task <User?> FindUser(string email)
    {
        string userKey = nameof(User) + "-" + email;
        User?  user    = await Cache.Retrieve <User>(userKey, TimeSpan.FromMinutes(15));

        if (user != null)
        {
            user = User.Find(email);
            if (user != null)
            {
                await Cache.Store <User>(false, userKey, user, TimeSpan.FromMinutes(15));
            }
            else
            {
                await Cache.Remove <User>(false, userKey);
            }
        }
        return(user);
    }
Example #2
0
        public async Task CacheNone()
        {
            using (ScopedLocalServiceOverride <IAmbientCache> localCache = new ScopedLocalServiceOverride <IAmbientCache>(null))
            {
                TestCache ret;
                AmbientCache <TestCache> cache = new AmbientCache <TestCache>();
                await cache.Store(true, "Test1", this);

                ret = await cache.Retrieve <TestCache>("Test1");

                Assert.IsNull(ret);
                await cache.Remove <TestCache>(true, "Test1");

                ret = await cache.Retrieve <TestCache>("Test1", null);

                Assert.IsNull(ret);
                await cache.Clear();

                ret = await cache.Retrieve <TestCache>("Test1", null);

                Assert.IsNull(ret);
            }
        }
Example #3
0
        public async Task CacheSpecifiedImplementation()
        {
            AmbientSettingsOverride localSettingsSet = new AmbientSettingsOverride(TestCacheSettingsDictionary, nameof(CacheSpecifiedImplementation));

            using (AmbientClock.Pause())
                using (new ScopedLocalServiceOverride <IAmbientSettingsSet>(localSettingsSet))
                {
                    TestCache                ret;
                    IAmbientCache            cacheService = new BasicAmbientCache(localSettingsSet);
                    AmbientCache <TestCache> cache        = new AmbientCache <TestCache>(cacheService, "prefix");
                    await cache.Store <TestCache>(true, "Test1", this);

                    ret = await cache.Retrieve <TestCache>("Test1", null);

                    Assert.AreEqual(this, ret);
                    await cache.Remove <TestCache>(true, "Test1");

                    ret = await cache.Retrieve <TestCache>("Test1", null);

                    Assert.IsNull(ret);
                    await cache.Store <TestCache>(true, "Test2", this, null, DateTime.MinValue);

                    ret = await cache.Retrieve <TestCache>("Test2", null);

                    Assert.AreEqual(this, ret);
                    await Eject(cache, 1);

                    ret = await cache.Retrieve <TestCache>("Test2", null);

                    Assert.IsNull(ret);
                    await cache.Store <TestCache>(true, "Test3", this, TimeSpan.FromMinutes(-1));

                    ret = await cache.Retrieve <TestCache>("Test3", null);

                    Assert.IsNull(ret);
                    await cache.Store <TestCache>(true, "Test4", this, TimeSpan.FromMinutes(10), AmbientClock.UtcNow.AddMinutes(11));

                    ret = await cache.Retrieve <TestCache>("Test4", null);

                    Assert.AreEqual(this, ret);
                    await cache.Store <TestCache>(true, "Test5", this, TimeSpan.FromMinutes(10), AmbientClock.Now.AddMinutes(11));

                    ret = await cache.Retrieve <TestCache>("Test5", null);

                    Assert.AreEqual(this, ret);
                    await cache.Store <TestCache>(true, "Test6", this, TimeSpan.FromMinutes(60), AmbientClock.UtcNow.AddMinutes(10));

                    ret = await cache.Retrieve <TestCache>("Test6", null);

                    Assert.AreEqual(this, ret);
                    ret = await cache.Retrieve <TestCache>("Test6", TimeSpan.FromMinutes(10));

                    Assert.AreEqual(this, ret);
                    await Eject(cache, 50);

                    await cache.Clear();

                    ret = await cache.Retrieve <TestCache>("Test6", null);

                    Assert.IsNull(ret);
                }
        }
Example #4
0
        public async Task CacheAmbient()
        {
            AmbientSettingsOverride localSettingsSet = new AmbientSettingsOverride(TestCacheSettingsDictionary, nameof(CacheAmbient));

            using (new ScopedLocalServiceOverride <IAmbientSettingsSet>(localSettingsSet))
            {
                IAmbientCache localOverride = new BasicAmbientCache();
                using (ScopedLocalServiceOverride <IAmbientCache> localCache = new ScopedLocalServiceOverride <IAmbientCache>(localOverride))
                {
                    TestCache ret;
                    AmbientCache <TestCache> cache = new AmbientCache <TestCache>();
                    await cache.Store(true, "Test1", this);

                    await cache.Store(true, "Test1", this);

                    ret = await cache.Retrieve <TestCache>("Test1", null);

                    Assert.AreEqual(this, ret);
                    await cache.Remove <TestCache>(true, "Test1");

                    ret = await cache.Retrieve <TestCache>("Test1", null);

                    Assert.IsNull(ret);
                    await cache.Store(true, "Test2", this, null, DateTime.MinValue);

                    ret = await cache.Retrieve <TestCache>("Test2", null);

                    Assert.AreEqual(this, ret);
                    await Eject(cache, 2);

                    ret = await cache.Retrieve <TestCache>("Test2", null);

                    Assert.IsNull(ret);
                    await cache.Store(true, "Test3", this, TimeSpan.FromMinutes(-1));

                    ret = await cache.Retrieve <TestCache>("Test3", null);

                    Assert.IsNull(ret);
                    await cache.Store(true, "Test4", this, TimeSpan.FromMinutes(10), DateTime.UtcNow.AddMinutes(11));

                    ret = await cache.Retrieve <TestCache>("Test4", null);

                    Assert.AreEqual(this, ret);
                    await cache.Store(true, "Test5", this, TimeSpan.FromMinutes(10), DateTime.Now.AddMinutes(11));

                    ret = await cache.Retrieve <TestCache>("Test5", null);

                    Assert.AreEqual(this, ret);
                    await cache.Store(true, "Test6", this, TimeSpan.FromMinutes(60), DateTime.UtcNow.AddMinutes(10));

                    ret = await cache.Retrieve <TestCache>("Test6", null);

                    Assert.AreEqual(this, ret);
                    ret = await cache.Retrieve <TestCache>("Test6", TimeSpan.FromMinutes(10));

                    Assert.AreEqual(this, ret);
                    await Eject(cache, 50);

                    await cache.Clear();

                    ret = await cache.Retrieve <TestCache>("Test6", null);

                    Assert.IsNull(ret);
                }
            }
        }