【问题标题】:How to cache a Memcached connection using the java spymemcached client如何使用 java spymemcached 客户端缓存 Memcached 连接
【发布时间】:2013-08-13 09:44:50
【问题描述】:

我正在学习如何使用 memcached 和来自 spymemcached examples 的 spymemcached 客户端缓存对象

MemcachedClient c=new MemcachedClient(new InetSocketAddress("hostname", portNum));    
// Store a value (async) for one hour
c.set("someKey", 3600, someObject);
// Retrieve a value (synchronously).
Object myObject=c.get("someKey");

我注意到,每次我想缓存或检索对象时,我都会创建一个新的 memcached 客户端,它假设是一个新连接,并且 memcached 没有连接池机制,因此建议用户缓存连接以减少开销从这个问题重新连接opening closing and reusing connections

我的问题是如何缓存此连接?有人可以给我一个我可以开始的例子。 如果您想知道我尝试了什么,我尝试将我的连接放在 memcached 中,但后来我意识到我必须创建一个连接才能获得它。 :)

提前致谢。

【问题讨论】:

    标签: memcached spymemcached


    【解决方案1】:

    我做了更多的研究,偶然发现了this question 然后我想出了我的解决方案,

    首先创建了一个上下文监听器

    public class ContextListener implements ServletContextListener {
    
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            Memcached.createClient();
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
        }
    
    }
    

    然后我通过将这些行添加到 web.xml 将侦听器添加到部署描述符中

    <listener>
        <description>Used with memcached to initialize connection</description>
        <listener-class>com.qualebs.managers.ContextListener</listener-class>
    </listener>
    

    我创建了一个类 Memcached 并添加了这些方法

        static void createClient() {
            try {
                client = new MemcachedClient(new InetSocketAddress("localhost", 11211));
            } catch (IOException ex) {
                Logger.getLogger(Memcached.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
            }
        }
    
        static MemcachedClient getClient() throws IOException {
            return client;
        }
    

    现在任何我需要使用 memcached 连接的地方只需调用 Memcached.getClient() 我希望这能帮助其他有同样问题的人。

    【讨论】:

      【解决方案2】:

      我注意到每次我想缓存或检索一个对象时,我 创建一个新的 memcached 客户端,假设是一个新连接

      不要这样做; spymemcache 对所有进出 memcache 的 I/O 使用单个连接;这一切都是异步完成的;来自 spymemcache 文档...

      每个 MemcachedClient 实例建立并维护一个单一的 连接到集群中的每台服务器。

      只需在您的应用中执行以下一次;确保客户端可用于您应用中的其他服务,以便他们可以访问它。

      MemcachedClient memClient = new MemcachedClient(new InetSocketAddress(host, port)); 
      

      使用memClient 进行内存缓存的I/O;无需每次都创建一个新的 MemcachedClient 实例;完毕。您提供的链接回答了您的所有问题。

      您的部署是什么?网络应用还是独立的?

      【讨论】:

      • 我正在部署一个网络应用程序。我应该在我的 serverlet 的 init() 方法中创建连接,然后将其传递给其他类,对吗?
      • 我现在已经解决了这个问题,我正在发布答案。感谢您为我指明了正确的方向。
      【解决方案3】:

      这只是意味着您应该重用您打开的连接,而不是为每个请求打开一个连接。将连接实例存储在 memcached 中是没有意义的。

      在这种情况下缓存连接意味着将其缓存在您的应用程序中(将其保存在内存中并打开),而不是将连接实际存储在 memcached 中。

      【讨论】:

      • 但这并不能真正回答我的问题。 This just means that you should use reuse the connections that you open as opposed to opening a connection for each request. It doesn't make sense to store a connection instance in memcached. 你只是转述了它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多