【问题标题】:How to create cache in Java to store user sessions如何在 Java 中创建缓存以存储用户会话
【发布时间】:2012-05-01 10:31:20
【问题描述】:

我想在 java 中创建缓存来存储用户会话。它类似于缓存,例如将为每个用户存储 5 个元素。我需要某种必须能够记住这些数据的 java 数据结构。到目前为止,我创建了这个 Java 代码:

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SessionCache {

    public SessionCache() {
    }

    /* Create object to store sessions */
    private List<ActiveSessionsObj> dataList = new ArrayList<>();

    public static class ActiveSessionsObj {
        private int one;
        private int two;
        private int three;
        private int four;
        private int five;

        private ActiveSessionsObj(int one, int two, int three, int four, int five) {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    }

    public List<ActiveSessionsObj> addCache(int one, int two, int three, int four, int five){

        dataList.add(new ActiveSessionsObj(
                        one,
                        two,
                        three,
                        four,
                        five));
          return dataList;    
    }   

}

我是 java 新手,我需要帮助我如何向结构中添加数据以及如何从结构中删除数据。我需要使用密钥来执行此操作。这可能吗?或者有没有更合适的数据结构来根据mu的需要来存储数据?

最好的祝福

【问题讨论】:

    标签: java arrays data-structures arraylist


    【解决方案1】:

    大概每个用户都有一个唯一的 id,所以Map 实现似乎是一个明智的选择,其中键是用户 id,值是 ActiveSessionsObj

    Map<String, ActiveSessionsObj> cache =
        new HashMap<String, ActiveSessionsObj>();
    

    请参阅 Javadoc 以从 Map 添加 (put()) 和删除 (remove()) 元素:

    public void addCache(String user_id,int one,int two,int three,int four,int five)
    {
        // You may want to check if an entry already exists for a user,
        // depends on logic in your application. Otherwise, this will
        // replace any previous entry for 'user_id'.
        cache.put(user_id, new ActiveSessionsObj(one, two, three, four, five));
    }
    

    【讨论】:

    • 我需要这样的东西:pastebin.com/wELvi0e9 但 netbeans 显示错误。插入数据的适当方式是什么?
    • @PeterPenzov,以添加到Map 的示例更新答案。
    • 我更新了代码pastebin.com/dkkBaXhY 有什么遗漏吗?我不知道我必须在公共 ActiveSessionsObj 中插入什么(int 1,int 2,int 3,int 4,int 5)
    • 彼得,你想要什么,你的设计。
    【解决方案2】:

    基本上,您不需要SessionCache 中的列表,只需定义一些私有属性,并提供一些重新使用的get set 方法来访问这些属性。

    【讨论】:

      【解决方案3】:

      您应该使用Map 接口的实例来存储您的数据对象。您需要确保每个用户都有一个唯一的密钥;如果你这样做了,你可以使用这个键作为HashMap的输入

      另外,为了减少 SessionCache 对 ActiveSessionsObj 内部细节的依赖,您应该让 addCache 方法采用 ActiveSessionsObj 之一。使用地图实现,这看起来更像:

      public void addCache(String key, ActiveSessionsObj data){
      
          dataMap.put(key, data);
      
      }   
      

      最好不要从 SessionCache 返回 Map,否则会破坏缓存的 encapsulation

      【讨论】:

      • 你能告诉我这段代码是否正确:pastebin.com/xdfEFGf6 现在我没有运行的服务器来测试它。
      • 我觉得不错。如果您知道您的缓存将只处理 ActiveSessionsObj,您可以使 getCache 对象返回一个 ActiveSessionsObj 而不仅仅是一个对象。此外,正如我之前所说,如果 addCache 方法只使用 ActiveSessionsObj 而不是一堆东西来构造一个,它将使您的缓存更加灵活。想想当您需要向会话对象添加新数据项时会发生什么。
      • 另外,如果您在服务器上运行它,并且您可能有多个线程同时访问缓存,您应该注意缓存的线程安全。最简单的事情是让所有公共方法synchronized.
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-16
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多