-1

Through controller I send to Cache a java list List

@for(index <- play.api.cache.Cache.get("nrOfmessages")){
<h1>@index</h1>
}

I see my item in list but I dont know how to iterate over this list from cache and how to fetch this list

Also I heard thar Cahche is not a good place for storing information, but i need some place to deliver information to my template...Unfortunately I cannot use session cause session has only 4kb size and i need something more :) Maybe sombody know how to transfer this sort of data to my template better? :)

Michal
  • 109
  • 1
  • 9

1 Answers1

1

Don't use cache to store data because cache not remain all time you may get null values.

From Play Docs

It is important to understand that the cache contract is clear: when you put data in a cache, you can’t expect that data to remain there forever. In fact you shouldn’t. A cache is fast, but values expire, and the cache generally exists only in memory (without persistent backup)

From Similar Stack question

Play is stateless, you should not use the Cache as a data store. If you are load balancing, there is no guarantee that you will return to the same server. By keeping things in memory, you are breaking the stateless nature of Play. The cache is best used when data already exists in the database, but is being used to minimise frequent database reads

Solution 1:Pass List to your view

In your controller

public static Result index() {
        List<String> message= new ArrayList<String>(); 
        message.add("message1");
        message.add("message2");
        message.add("message3");
        return ok(index.render(message));
    }

In your view

@(messages:List[String])

@main("index"){

@for(msg <- messages){
<div>@msg</div>
}

}

And you can always take help from sample projects provided by play which is in the zip file that you downloaded and extracted.

Note: You can pass variable from controller to your view in play by passing the variable like you pass variable between functions in java.

Solution 2:Directly call the models function

In your view

@for(user<- User.all()){
<div>@user.getName</div>
}

where User is a model and all() is a function which returns list of users.

Solution 3:Use ajax call to get the list

See how to apply ajax in Play framework

Community
  • 1
  • 1
singhakash
  • 7,891
  • 6
  • 31
  • 65
  • Unfortunately I can't pass my variable throught my template functions because I've already used it in other cases. – Michal Jan 29 '15 at 08:36
  • @Michal If you dont want to pass variables and dont want to use session then you have another option of using a ajax call and directly call model function(see my edits) .Storing variable in cache is not a good practice and Its not good for future in your project as the pages may crash. – singhakash Jan 29 '15 at 10:57
  • That was fantastic hint...I totally forgot about ajax existance thx a lot :) – Michal Jan 29 '15 at 15:13
  • @Michal have you tried `solution 2` from my answer that would be lot easier – singhakash Jan 29 '15 at 15:20
  • thanks for help... User.all() - this is a method from controller? I think I can't use it because in this case I should use reverse routing...Could you say something more ? Some example would be very helpful :d – Michal Jan 29 '15 at 15:44
  • Oh...i got it right now...I think this is connected with EBean...unfortunately in my project I've decided to use JPA instead of Ebean (bigger community) – Michal Jan 29 '15 at 15:46
  • User is a model and all() is a function in that model(you can call function from model by ModelName.functionname() same as you call function in java) I have tried this `solution 2` with Ebean and JPA .I dont know if it the correct way or not but it worked for me. – singhakash Jan 29 '15 at 15:56