Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching with Play framework and Java

I am running an application with Play and Java, and I need to set up expiration date for various types of assets: images, css, javascript etc.

I have the following in the conf/routes file:

GET     /assets/*file               controllers.Assets.at(path="/public", file)

I was able to set expiration date for one individual file in application.conf:

"assets.cache./public/js/pages/validation.js"="max-age=7200"

But I am not able to set it for a whole folder. I have tried

"assets.cache./public/js/pages/*.js"="max-age=7200"
"assets.cache./public/js/pages/*"="max-age=7200"

but nothing happens. I was hoping to set the expiration date for everything in the /js/pages folder.

I've also tried

assets.defaultCache="max-age=7200"

per instructions at http://www.jamesward.com/2014/04/29/optimizing-static-asset-loading-with-play-framework

as well as

http.cacheControl=7200

per documentation http://www.playframework.com/documentation/1.2.3/configuration#http

and none of these work. The changes above were done in application.conf.

I know there is a way to do the same by defining controllers that change the response() for the routes that I want to set the expiration date for: far future Expires header for static contents

But I would like to know how to configure expiration date for assets from the application.conf file.

Our application is running on S3 Linux instances, so configuring the expire date on the server is not an option.

Thank you!

like image 917
sempervirescent Avatar asked Jun 03 '14 16:06

sempervirescent


People also ask

What are caching framework in Java?

Object Caching Service for Java provides caching for expensive or frequently used Java objects within Java programs. The Object Caching Service for Java automatically loads and updates objects as specified by the Java application. And finally, Oracle iCache Data Source provides data caching within the database server.

What is caching and types of caching in Java?

In-memory Caching It is the area that is frequently used. Memcached and Redis are examples of in-memory caching. It stores key-value between application and database. Redis is an in-memory, distributed, and advanced caching tool that allows backup and restore facility.

Does Java have a cache?

The Java Object Cache provides caching for expensive or frequently used Java objects when the application servers use a Java program to supply their content. Cached Java objects can contain generated pages or can provide support objects within the program to assist in creating new content.


1 Answers

Play framework does not support "assets.cache./public/js/pages/*.js"="max-age=7200" but assets.defaultCache="max-age=7200" should work.

In debug/dev mode (starting app using play run) assets.defaultCache is ignored, so it is always 'no-cache'. Make sure you are running it in prod mode(using play start).

I can't find any reference in docs, but same can be checked in https://github.com/playframework/playframework/blob/master/framework/src/play/src/main/scala/play/api/controllers/Assets.scala AssetInfo::cacheControl function

like image 100
a-- Avatar answered Sep 21 '22 23:09

a--