- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 netty 编写一个服务器,我需要在其中调用 memcached。我正在使用 spymemcached 并且可以轻松地进行同步 memcached 调用。我希望这个 memcached 调用是异步的。那可能吗? netty 提供的示例似乎没有帮助。
我尝试使用回调:创建了一个 ExecutorService
我的处理程序中的池并将回调工作程序提交到此池。像这样:
public class MyHandler extends ChannelInboundMessageHandlerAdapter<MyPOJO> implements CallbackInterface{
...
private static ExecutorService pool = Executors.newFixedThreadPool(20);
@Override
public void messageReceived(ChannelHandlerContext ctx, MyPOJO pojo) {
...
CallingbackWorker worker = new CallingbackWorker(key, this);
pool.submit(worker);
...
}
public void myCallback() {
//get response
this.ctx.nextOutboundMessageBuf().add(response);
}}
CallingbackWorker
好像:
public class CallingbackWorker implements Callable {
public CallingbackWorker(String key, CallbackInterface c) {
this.c = c;
this.key = key;
}
public Object call() {
//get value from key
c.myCallback(value);
}
this.ctx.nextOutboundMessageBuf()
在
myCallback
卡住。
最佳答案
这里有两个问题:一个与您尝试编码的方式有关的小问题,以及一个具有许多提供异步服务调用的库的大问题,但没有在异步框架中充分利用它们的好方法,例如内蒂。这会迫使用户进行像这样的次优黑客攻击,或者是一种不太糟糕但仍然不是我稍后会介绍的理想方法。
首先是编码问题。问题是您试图从与处理程序关联的线程以外的线程调用 ChannelHandlerContext 方法,这是不允许的。这很容易修复,如下所示。您可以通过其他几种方式对其进行编码,但这可能是最直接的方式:
private static ExecutorService pool = Executors.newFixedThreadPool(20);
public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
//...
final GetFuture<String> future = memcachedClient().getAsync("foo", stringTranscoder());
// first wait for the response on a pool thread
pool.execute(new Runnable() {
public void run() {
String value;
Exception err;
try {
value = future.get(3, TimeUnit.SECONDS); // or whatever timeout you want
err = null;
} catch (Exception e) {
err = e;
value = null;
}
// put results into final variables; compiler won't let us do it directly above
final fValue = value;
final fErr = err;
// now process the result on the ChannelHandler's thread
ctx.executor().execute(new Runnable() {
public void run() {
handleResult(fValue, fErr);
}
});
}
});
// note that we drop through to here right after calling pool.execute() and
// return, freeing up the handler thread while we wait on the pool thread.
}
private void handleResult(String value, Exception err) {
// handle it
}
// in some globally-accessible class:
public static final ForeignFutureTracker FFT = new ForeignFutureTracker(1, TimeUnit.MILLISECONDS);
// in a handler class:
public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
// ...
final GetFuture<String> future = memcachedClient().getAsync("foo", stringTranscoder());
// add a listener for the Future, with a timeout in 2 seconds, and pass
// the Executor for the current context so the callback will run
// on the same thread.
Global.FFT.addListener(future, 2, TimeUnit.SECONDS, ctx.executor(),
new ForeignFutureListener<String,GetFuture<String>>() {
public void operationSuccess(String value) {
// do something ...
ctx.fireChannelRead(someval);
}
public void operationTimeout(GetFuture<String> f) {
// do something ...
}
public void operationFailure(Exception e) {
// do something ...
}
});
}
关于memcached - 如何在netty中异步访问memcached,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17661382/
memcached 能否充分利用多核?或者有什么办法可以调整这个? 最佳答案 默认情况下,memcached 是多线程的,并且在使许多内核饱和时没有问题。在更大规模的并行机器(例如 256 核 CMT
有没有办法根据通配符键使内存缓存中的条目无效? 因此,如果我有以下内存缓存键: data/1 data/2 data/3 有没有一种方法可以使这些 key 失效,例如 data/* ?一口气清除一堆陈
Memcache 可以配置为使用磁盘存储而不是 RAM 吗? 我在 High IO Amazon EC2 实例上运行 Memcache Server。该实例有 2TB 的 SSD 存储可用。我可以将
memcached 的最大 key 过期时间是多少? 如果我不提供过期时间并且缓存已满,会发生什么? 最佳答案 您可以通过提供 Unix 时间戳而不是天数来将 key 过期设置为某个日期。该日期可以是
我有一个运行 php/mysql 的网站。该应用程序将被多次“复制”。每个实例都会获得自己的数据库、一组 php 文件(通配符)域,您明白了。 现在应用程序的某些部分是使用 memcache 进行缓存
是否可以根据某些正则表达式获取 memcached key 列表?我知道一种解决方案是将 key 存储在数据库中,并在需要删除这些 key 时获取列表。这意味着,它将给数据库带来额外的成本。 我想知道
我们发现使用我们的 memcache 时,一些键往往会丢失。为了调试,我使用命令 stats cachedump 输出是这样的: ITEM key-stuff-123-mlgtr-tmura [5
从 Network Solutions 移至托管站点,出现错误 fatal error :第 6 行 上/data/.../include/connect.inc 中未找到类“Memcache” 我该
我遵循了这个 tutorial 中的说明关于如何安装 nginx、php 和 mysql,包括 php5-memcache。 我想知道我是否还需要安装没有“php5”前缀的 memcache 或 me
我在 ubuntu 12.04 上运行 apache/php/memcached 我设置的 key 在过期后仍然有效。我们使用的代码与我们从中迁移的内存缓存 PAAS 的代码相同。 我如何确认过期实际
假设我在开始时有两个 memcached 节点( 节点 A, B ),当我添加一个新的 时节点 C ,部分键被重新映射,并且由于一致散列只有其中一些。 让我们假设一个键为“ foo ”的值原来在服务器
真的,我只想知道memcached中的slab是什么。如果有认识的同事能回答我的话会更好。 谢谢你的回答... 最佳答案 长时间运行的应用程序(如 memcached)会遇到内存碎片问题,服务运行时间
在 memcached 概述中 here 它说: Memcached servers are generally unaware of each other. There is no crosstal
当您在 memcached 中放入一个值时,您可以设置一个过期时间。 但是,当您增加一个值时,您不能(至少不是来自 PHP )设置新的到期时间。 我的问题:到期时间是否在递增时重置为其初始值?或者它没
我在我的 centos 服务器上使用 memcached,我的项目很大并且有超过 1MB 的对象我需要保存到 memcached,好吧,我不能!因为 max_item_size 是 1MB,无论如何要
我尝试使用本教程在 MAMP - Snow Leopard 10.6.2 中安装 memcached: Setup a Memcached-Enabled MAMP Sandbox Environme
我试图添加的方式 -l 11211 -l 11212 在 memcached conf 文件中。但它只是在听第一个,即 1121 最佳答案 首先我使用了 mikewied 的解决方案,但后来我遇到了自
我希望记录我的 memcached 服务器中正在进行的所有事件。所有读取和写入。 这将用作云中许多远程 php 应用程序的分布式守护程序,并且需要一种通过 SSH 登录并检查守护程序上正在进行的事件的
我在使用 memcached 时遇到了一些问题,我的一个想法是它可能已经在我尝试运行它的端口上运行,由我们网络上的其他用户启动。有没有办法知道当前正在使用哪些 memcached 端口? 最佳答案 要
运行 4 个 .5GB 内存缓存服务器或运行一个 2GB 实例有什么区别吗? 运行多个实例有什么好处吗? 最佳答案 如果一个实例失败,您仍然可以享受使用缓存的优势。如果您使用一致性哈希将相同的数据引入
我是一名优秀的程序员,十分优秀!