标签: memorycache

如何在Android中实现ListView缓存

我有一个包含大量数据的ListView.
第一次,我从Webservice加载所有数据.

现在我想缓存这些数据,这样,如果我要再次打开该页面,我可以从缓存中获取数据,而不是再次查询Web服务.

我怎么做?.

android caching memorycache

5
推荐指数
1
解决办法
6202
查看次数

ASP.NET MemoryCache跨应用程序共享

我一直在阅读关于从.Net Framework 4.0开始的新MemoryCache类的所有地方.根据我的阅读,您可以跨不同的.Net应用程序访问MemoryCache.我试图在Asp.Net应用程序和标准的Windows窗体.Net应用程序之间共享一个对象.如果我将对象添加到.Net应用程序中的MemoryCache,则Asp.Net应用程序看不到它.有没有办法实现这个目标?感谢您的时间,非常感谢.

Windows窗体应用程序:

    Dim cache As ObjectCache = MemoryCache.Default
    Dim policy As New CacheItemPolicy()
    policy.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(60)
    cache.Set("testcache", TestObj, policy)
Run Code Online (Sandbox Code Playgroud)

Asp.Net App:

    Dim cache As ObjectCache = MemoryCache.Default
    If IsNothing(cache("testcache")) Then Response.Write("TestCache Is Nothing")
Run Code Online (Sandbox Code Playgroud)

谢谢 - 瑞恩

.net vb.net asp.net system.web.caching memorycache

5
推荐指数
2
解决办法
4522
查看次数

AddOrGetExisting不会将过期时间考虑在内

我正在使用(.NET 4.5)MemoryCache,与SlidingExpiration结合使用.

我注意到方法.AddOrGetExisting()似乎没有考虑到期,而.Get()确实如此.

单元测试:

    [TestMethod]
    public void NonWorking()
    {
        var memCache = new MemoryCache("somekey");
        var cachePolicy = new CacheItemPolicy() { SlidingExpiration = TimeSpan.FromSeconds(1) };

        var cacheEntry = memCache.AddOrGetExisting("key1", "foo", cachePolicy);
        Assert.AreEqual(null, cacheEntry); // OK: AddOrGetExisting returns null, because it wasn't existing yet
        Thread.Sleep(1100);

        // Expecting null, since the existing item for key1 has expired by now.
        // It is, however, still "foo".
        Assert.AreEqual(null, memCache.AddOrGetExisting("key1", "bar", cachePolicy));

        // FYI: afterwards, memCache.Get("key1") still equals "foo"
    }

    [TestMethod]
    public void Working()
    {
        var …
Run Code Online (Sandbox Code Playgroud)

.net c# memorycache

5
推荐指数
1
解决办法
1994
查看次数

内存缓存中的Dotnet Core-默认过期时间是多少

我在dotnet核心C#项目中使用MemoryCache。我正在使用它存储我从集合中读取的枚举列表(我想存储它,因为它使用反射来加载枚举,这需要时间)。

但是,由于我的收藏集不会改变,所以我根本不希望它过期。我怎么做?如果我没有设置任何过期时间(SlidingExpiration或Absolute Expiration),我的缓存将永不过期吗?

memorycache asp.net-core-mvc

5
推荐指数
1
解决办法
2216
查看次数

.Net MemoryCache - 如何更新现有项目的到期时间?

Memcached API具有Touch()方法,该方法更新给定密钥的到期策略.如何使用.Net ObjectCache类最好地完成此操作?

我能看到的最好的方法是删除对象并重新添加新的过期,但显然这是次优的性能.

.net caching memorycache

4
推荐指数
1
解决办法
485
查看次数

普通缓存类和MemoryCache类有什么区别?

普通缓存类和MemoryCache类有什么区别?

缓存是指存储在内存中的数据。那么为什么要给MemoryCache额外的类呢?

MemoryCache 类的用途是什么?何时使用它来代替普通缓存类?

只需看下面的示例代码

private void btnGet_Click(object sender, EventArgs e)
{
    ObjectCache cache = MemoryCache.Default;
    string fileContents = cache["filecontents"] as string;

    if (fileContents == null)
    {
        CacheItemPolicy policy = new CacheItemPolicy();

        List<string> filePaths = new List<string>();
        filePaths.Add("c:\\cache\\example.txt");

        policy.ChangeMonitors.Add(new 
        HostFileChangeMonitor(filePaths));

        // Fetch the file contents.
        fileContents = 
            File.ReadAllText("c:\\cache\\example.txt");

        cache.Set("filecontents", fileContents, policy);
    }

    Label1.Text = fileContents;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码做了什么?是否监控文件内容变化?

asp.net memorycache

4
推荐指数
1
解决办法
4913
查看次数

从类型 Object 转换匿名类型

我正在尝试使用 .NET 4.0 中的 System.Runtime.Caching.MemoryCache 类。我有一个通用的方法,因此我可以将任何类型传递到内存缓存中,并在调用时将其取回。

该方法返回一个 object 类型的对象,该对象是具有包含缓存对象的字段 Value 的匿名类型。

我的问题是,如何将要返回的对象转换为其相应的类型?

下面是我的代码...

public static class ObjectCache
{
    private static MemoryCache _cache = new MemoryCache("GetAllMakes");

    public static object GetItem(string key)
    {
        return AddOrGetExisting(key, () => InitialiseItem(key));
    }

    private static T AddOrGetExisting<T>(string key, Func<T> valueFactory)
    {
        var newValue = new Lazy<T>(valueFactory);
        var oldValue = _cache.AddOrGetExisting(key, newValue, new CacheItemPolicy()) as Lazy<T>;

        try
        {
            return (oldValue ?? newValue).Value;
        }
        catch
        {
            _cache.Remove(key);
            throw;
        }
    }

    /// <summary>
    /// How can i access Value …
Run Code Online (Sandbox Code Playgroud)

.net c# generics memorycache

4
推荐指数
1
解决办法
3531
查看次数

点净内存缓存逐出

点网的MemoryCache驱逐何时发生?如何在控制台应用程序中模拟逐出?每当我尝试将对象添加到内存缓存中直到发生逐出时,我都会得到OutofMemoryException。

.net c# memorycache

4
推荐指数
1
解决办法
1358
查看次数

.Net MemoryCache Miss 当使用对象作为键时

使用时IMemoryCacheobjectTryGetValue永远怀念。我正在尝试将 atuple<string, object>作为关键,并且 atuple<string, string>工作得很好。

这里的代码总是让我缓存未命中:

_cache.TryGetValue(("typeOfCache", query), out var something);
if(something == null) _cache.CreateEntry(("typeOfCache", query));
Run Code Online (Sandbox Code Playgroud)

我使用的对象里面有列表列表,而不是没有字典/集合(没有随机排序的东西)。

这是 .net 错误还是我做错了什么?

.net c# caching memorycache

4
推荐指数
1
解决办法
2263
查看次数

当我达到大小限制时,为什么我需要在大小受限的 MemoryCache 上调用两次 Set ?

我们即将使用 ASP.NET Core 的内置内存缓存解决方案来缓存外部系统响应。(我们可能会从内存中转移到IDistributedCache以后。)
我们想使用Mircosoft.Extensions.Caching.Memory的,IMemoryCache正如MSDN 建议的那样

我们需要限制缓存的大小,因为默认情况下它是无界的。
因此,在将它集成到我们的项目中之前,我创建了以下 POC 应用程序来使用它。

我的自定义 MemoryCache 以指定大小限制

public interface IThrottledCache
{
    IMemoryCache Cache { get; }
}

public class ThrottledCache: IThrottledCache
{
    private readonly MemoryCache cache;

    public ThrottledCache()
    {
        cache = new MemoryCache(new MemoryCacheOptions
        {
            SizeLimit = 2
        });
    }

    public IMemoryCache Cache => cache;
}
Run Code Online (Sandbox Code Playgroud)

将此实现注册为单例

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddSingleton<IThrottledCache>(new ThrottledCache());
}
Run Code Online (Sandbox Code Playgroud)

我已经创建了一个非常简单的控制器来使用这个缓存。

用于玩 MemoryCache 的沙盒控制器

[Route("api/[controller]")]
[ApiController]
public class MemoryController : …
Run Code Online (Sandbox Code Playgroud)

c# memorycache asp.net-core asp.net-core-3.1

4
推荐指数
1
解决办法
515
查看次数