如何在Portlet实例的所有用户之间共享数据? [英] How to share data between all users of a portlet instance?

查看:81
本文介绍了如何在Portlet实例的所有用户之间共享数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Portlet实例的所有用户之间共享bean,但是我不知道如何.

I would like to share beans between all users of a portlet instance but I don't know how.

我正在研究一个(Liferay)Portlet,该Portlet将具有许多对该Portlet实例的每个用户有用的数据:将Portlet放置在页面上之后,它将根据其首选项从DB获取数据.我希望此Portlet将此数据存储在RAM中,而不希望将其存储在RAM中,以避免从String转换为String.

I am working on a (Liferay) portlet that will have many data useful for every users of an instance of this portlet : after being placed on a page, the portlet will get data from DB that depends on its preferences. I would like this portlet to store this data in RAM and not in preferences to avoid conversion from/to String.

我知道我可以将其存储在会话中,但这将为查看该Portlet的每个用户创建一个数据副本.结果:因此,我在RAM中可能有大量数据. 我不能使用上下文(或Portlet类中的局部变量),因为数据将在Portlet的所有实例之间共享,并且我希望它特定于实例.

I know I can store it in session, but this will create a copy of the data for each user viewing the portlet. Result : I could have a huge amount of data in RAM because of this. I can't use the context (or a local variable in my Portlet class) because the data would be shared between all instances of the portlet, and I want it to be specific for an instance.

事实上,我希望它可以像portlet的首选项一样工作,但可以使用Strings之外的其他bean.

In facts, I would like to have it working just like the preferences of the portlet, but with other beans than Strings.

这是我想要的示例(比如说我的portlet正在显示内容列表,就像Asset Publisher一样):

Here is an example of what I would like (saying my portlet is displaying a list of contents, just like the Asset Publisher) :

  • 在页面上,我将放置portlet的2倍:一个显示所有事件,另一个显示所有博客条目
  • 设置首选项后,portlet的每个实例将获得内容列表并将其存储在RAM中
  • 当用户访问页面时,将使用每个portlet实例的内容列表
  • 当另一个用户访问同一页面时,将使用相同的列表(例如,相同列表"是指List的相同实例)

下面是Portlet类的代码示例:

Here an example of code for Portlet class:

package com.test;

import com.liferay.portlet.asset.model.AssetEntry;
import com.liferay.util.bridges.mvc.MVCPortlet;

import java.io.IOException;
import java.util.List;

import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

/**
 * Portlet implementation class NewPortlet
 */
public class NewPortlet extends MVCPortlet {
    @Override
    public void doView(RenderRequest renderRequest,
            RenderResponse renderResponse) throws IOException, PortletException {

        List<AssetEntry> entries = null;

        /*HERE : Get the entries from RAM (session/context/...)*/

        renderRequest.setAttribute("entries", entries);

        super.doView(renderRequest, renderResponse);
    }
    /*HERE : The methods containing code for preferences, setting "entries" to RAM, etc...*/
}

有人知道怎么做吗,还是不可能?

Does anyone knows how to do it, or if it's impossible?

推荐答案

通读所有注释以及将数据存储在RAM中的需求,这听起来很像是对我的初步优化.

Reading through all of the comments and your demand to store the data in RAM, this sounds a lot like preliminary optimization for me.

您这样做是因为已经测量了从其他来源读取数据的影响?如果您在这里谈论Liferay实体(博客文章等),那么它们实际上是被缓存的:如果通过API检索它们,则很可能不会访问数据库,而无论如何只是访问缓存,而无需保留它们存储在内存中,并可能失去对基础数据的后续更改.

Are you doing this because you have measured the impact of reading the data from other sources? If you're talking about Liferay Entities here (blog posts etc) then they're actually cached: If you retrieve them through the API, you'll most likely not hit the database but just access the cache anyways, without any need to keep them in memory and risk to loose subsequent changes to the underlying data.

此外,如果您不参考Liferay实体,您确定这是应该在Portlet级别上解决的问题吗?如果您具有业务级别"数据,则可能希望将它们放在业务层中(可能已缓存),并仅通过ID引用它们-这些ID将很容易存储在Portlet首选项中.

Also, in case you're not referring to Liferay entities, are you sure that this is a problem that should be solved on the portlet level? If you have "business level" data, you might want to have them in your business layer (maybe cached) and just refer to them by Ids - and these Ids would easily be stored in portlet preferences.

技术信息:

由于Portlet首选项是由门户网站存储的,并且仅为字符串指定(真正的bean会触发各种类加载问题),因此您对这种类型的限制是正确的.除了字符串,您要保留的所有内容都必须由您自己处理.而且最有可能-正如我在上面说的-这最适合业务层而不是UI.

As portlet preferences are stored by the Portal - and specified only for Strings (real beans would trigger all kinds of classloading issues) - you're right with the limitations to this type. Anything you want to persist beyond Strings has to be handled by you yourself. And most likely - as I said above - this suits best for the business layer, not the UI.

这篇关于如何在Portlet实例的所有用户之间共享数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆