Forum Discussion

Inactive User's avatar
Inactive User
8 years ago
Solved

usercache: where's the earliest place I can usercache.put? Page Init doesn't seem to work.

I have a test to do on user roles and groups that I'd like to do as early as possible on page load, and keep it in the cache. (i.e., what private groups are they allowed access to). Right now I'm using LiQL like so:

 

SELECT count(*) FROM categories WHERE depth = 2 and parent_category.id = 'private-support'

This results in all the private categories they have access to, usually one. Within those, there are either 

  1. Discussion, Ideas, a TKB, or
  2. Just a Lithium group

Case 1: continue to the Category (i.e., ClientName Private Support)

Case 2: If it's just the group I am redirecting the page to the actual group rather than the parent category containing it, for user simplicity. This is why it's in page init, so the redirect can happen on server side before any HTML is written

 

Since I'm running the query there, I'd like to be able to use usercache.put in the page init to keep that for other components to use rather than repeat the queries, but it doesn't seem to work. Is that by design? A bug? 

 

If it's not permissible in page init, where else can I put that query so that it happens as early as possible?

10 Replies

  • Inactive User

    Page Initialization Script is the best option that enables you to execute business logic using FreeMarker early in a request before any HTML being rendered.  Usercache will also work on page init.  Might be you are missing something in your code snippet. 

     

    Here you can get some more insight view about page init and usercache 

    https://community.lithium.com/t5/Developer-Knowledge-Base/Cache-the-output-of-a-macro/ta-p/182354

    https://community.lithium.com/t5/Developer-Knowledge-Base/Using-a-FreeMarker-Page-Initialization-Script/ta-p/125767

  • VikasB's avatar
    VikasB
    Boss
    8 years ago

    TariqHussain Thanks for the sharing documents but might be it is not updated.  As I tested it and working fine on page init.

  • Inactive User's avatar
    Inactive User
    8 years ago

    Thanks for the reply. 

    I'm trying use pass some results from a LiQL query in the page Common.init.ftl, for example:

     

    <#assign permittedBoards = "Some value"/>
    <#assign
    pb = usercache.put("permittedBoards",permittedBoards)/>

     

    But when I try to retrieve it in an unrelated component downstream, the value is null. 

     

        <#assign permittedBoards = usercache.get("permittedBoards")!"oops"/>

     

  • VikasB's avatar
    VikasB
    Boss
    8 years ago

    Inactive User  Perfect. I tested it and working fine in our instance(see SS).

     

    There may be some issue in Liql query. I would suggest you, once give it a shot with a static value instead of Liql query response.

  • Inactive User's avatar
    Inactive User
    8 years ago

    VikasB I still am having no luck. 

     

    Common.init.ftl:

     

        <#assign pCats = "SELECT count(*) FROM categories WHERE depth = 2 and parent_category.id = 'private-support'" />
        <#assign pCatsCount = rest("2.0", "/search?q=" + pCats?url).data.count![]  />
    
        <#assign testCacheA = usercache.put("testCacheA",pCatsCount?string!"ERROR")/>
        <#assign testCacheB = usercache.put("testCacheB","this is testCacheB"?string!"ERROR")/>

     

    SomeComponent.ftl

    <#assign testCacheA = usercache.get('testCacheA') />
    <#assign testCacheB = usercache.get('testCacheB') />
    
    <p><strong>testCacheA: ${testCacheA?string!"null"}</strong></p>
    <p><strong>testCacheB: ${testCacheB?string!"null"}</strong></p>

     

    Results Screenshot:

     

     

     

  • VikasB's avatar
    VikasB
    Boss
    8 years ago

    Inactive User  It should work. I just pasted the same code and it works fine.  Can you try it inside studio component? 

     

    See screenshots

    SS1: Component on page

     

    SS2: Component Inside studio

     

  • Inactive User's avatar
    Inactive User
    8 years ago

    VikasB You are correct, there was a constraint in the code ( <#if user.ranking ....></#>) that I kept skipping over. Thanks for all the support!

  • Inactive User's avatar
    Inactive User
    8 years ago

    VikasB I guess the question then becomes, "what data is available about the page and user at Common.init.ftl?"

    Is it everything you could access once the page is loaded or are there limitations? Thanks :)

  • VikasB's avatar
    VikasB
    Boss
    8 years ago

    Inactive User

    Most of the required objects can be used insideth page init script. 

    Here is the list of context objects. Refer to the table to see which objects can be used where.