简体   繁体   English

在c#中使用redis缓存时出现任务取消错误

[英]Task cancelled error when using redis cache in c#

I build an out-of-process cache for my webpage's database.我为我的网页数据库构建了一个进程外缓存。 Though when I try to do something with it (Set, Get), I get the following error:虽然当我尝试用它做一些事情(设置、获取)时,我收到以下错误:

A task was canceled

Here's my redis cache code.这是我的 redis 缓存代码。 any help would be great.任何帮助都会很棒。 thanks谢谢

public class RedisCache : ICache
{
    private RedisConnection redis;

public RedisCache()
{
    redis = new RedisConnection("127.0.0.1");
    redis.Open();
}

public object Get(string key)
{
    var method = redis.Strings.Get(0, key);
    if (method == null)
        return null;
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream(method.Result);
    object obj = bf.Deserialize(ms);
    return obj;
}

public void Set(string key, object value)
{
    MemoryStream ms = new MemoryStream();
    BinaryFormatter bf = new BinaryFormatter();
    bf.Serialize(ms, value);
    redis.Strings.Set(0, key, ms.ToArray());
}

} }

The "cancelled" status means that a message was queued but could not be written to the stream (for example, the stream never opened). “已取消”状态表示消息已排队但无法写入 stream (例如,stream 从未打开)。 From 1.3 onward, there are 2 main scenarios for this:从 1.3 开始,有两个主要场景:

  • the message was queued because the connection was not open , and it was later discovered that the connection was impossible消息因连接未打开而排队,后来发现无法连接
  • a transaction was not issued because a pre-condition failed, or was aborted because "watch" key was changed由于先决条件失败而未发出交易,或由于“watch”键已更改而中止

Since you aren't using transactions, it sounds like the connection couldn't be opened in the first place.由于您没有使用事务,因此听起来一开始就无法打开连接。 You can check this by looking at the Task you get back from Open() - at the simplest:您可以通过查看从Open()返回的Task来检查这一点——最简单的方法是:

redis.Wait(redis.Open());

The Wait method here is like the usual task.Wait() , but it has inbuilt timeout support, and a few other things to make life handy - I do encourage it's usage (mainly for convenience);这里的Wait方法就像通常的task.Wait()一样,但它有内置的超时支持,以及其他一些让生活变得方便的东西——我鼓励使用它(主要是为了方便); likewise, redis.Wait(method) would be preferable to method.Result - but either will usually work fine.同样, redis.Wait(method) method.Result可取 - 但通常都可以正常工作。 You could also await or ContinueWith the task - the key point here is that you need to check that it opened - and the only way to do that is by seeing what happens with the Task .您也可以awaitContinueWith任务 - 这里的关键点是您需要检查它是否打开- 唯一的方法是查看Task发生了什么。

Note that the connection has some events for detecting failure (errors and closure).请注意,连接有一些事件用于检测故障(错误和关闭)。 You may also find it conveneint to open the connection with ConnectionUtils , which has some inbuilt handling for a range of common scenarios.您可能还会发现使用 ConnectionUtils 打开连接很方便, ConnectionUtils具有针对一系列常见场景的一些内置处理。

On final observation: BinaryFormatter ... you may find when you version / refactor your API you can't load your old data - don't say I didn't warn you;p I would recommend any contract based serializer instead: XmlSerializer , DataContractSerializer , JSON.NET, or protobuf.net (the latter being dense binary - ideal for opaque out-of-process BLOB such as redis, although I hear the author is nothing but trouble).最后观察: BinaryFormatter ......你可能会发现当你版本化/重构你的 API 时你无法加载你的旧数据 - 不要说我没有警告你;p我会推荐任何基于合同的序列化程序: XmlSerializerDataContractSerializer 、JSON.NET 或 protobuf.net(后者是密集二进制 - 非常适合不透明的进程外 BLOB,例如 redis,尽管我听说作者只是麻烦)。

I am using redis cach, in task.Delay in am passing cancellation token and set in redis cache in second operation i am getting that cache and want to cancel that cancellation token which is not actually cancelling token….我在任务中使用 redis 缓存。延迟传递取消令牌并在第二次操作中设置 redis 缓存我正在获取该缓存并想要取消实际上并没有取消令牌的取消令牌...... But same this is prefectly working with inmemory cache但同样这完全适用于内存缓存

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM