Showing posts with label cache. Show all posts
Showing posts with label cache. Show all posts

Monday, May 26, 2014

seconds_until_end_of_day expires for Cache in Rails

A colleague of mine had to cache some html until the end of the day but found it difficult because cache expects expires_in which is relative. We cant pass an absolute Time.now :P

So we now have seconds_until_end_of_day in 4.0.2, but we use Rails 3 :(

But patching Time is simple

def seconds_until_end_of_day
  end_of_day.to_i - to_i
end

http://apidock.com/rails/v4.0.2/Time/seconds_until_end_of_day

Monday, March 10, 2014

Caching Simple Navigation

Morale: Simple Navigation is not that simple

We, in our firm, have different types of user navigation for different types of users. We wanted the flexibility of creating our navigation from the backend(rails) so that there is just one file to maintain with all configurations. Simple Navigation works great in this aspect.

The configuration is complex, but is absolutely generic. We have a couple of navigations to start with, for the normal application and when a user visits the application via the iPad.

The navigation for the normal user takes more than 500ms to render. NOT GOOD! We decided on caching the html. For this we need to split the navigation yet again. With respect to logged in users. Therefore we now have 4 types of navigation, loggedout-normal, loggedin-normal, loggedout-iPad, loggedin-iPad.

We also had some badges on the navigation for unopened items. The next step was to remove these from the navigation file. We created a notifications controller and wrote a small javascript, which takes data from the json response of the show method of this controller, and updated the corresponding sections in the navigation.

Now came the hard part. Caching.

We need to define keys for the Caching itself first.

For a logged out normal user, this would be quite straight forward. Get the locale of the navigation bar. All the users with the same locale got the same navigation bar.

For logged out normal users it would be a little tricky. We have some user specific stuff in the navigation bar such as the username. Therefore, we need the user_id. The user could change his locale and this would trigger a change in the updated_on field on the Users table. This would mean that we need to include this in the key as well. We have a specific link for backend services if the user has admin privileges. For this we need to evaluate the roles of the user. An MD5 has been used to facilitate this.

We need to Cache the non highlighted navigation bar for sanity purposes. Then we plan to highlight the section according to the pages that are rendered. For this we could either create a javascript to add the correct classes or create a stylesheet which highlights the correct section on verification of the correct classes.

We decided to create a stylesheet for this. The stylesheet reads the configuration from the simple_navigation configuration and creates the correct CSS. The file is a LESS file. We extracted the styles to highlight the navigation items into some mixins and used these mixins inside our custom classes.

The next tough part is the sub navigation. While this problem still lurks, we are trying to get away from the sub navigation altogether.

While this post is clearly not technical and is conceptual, this might help some others trying to solve something similar to this.

:)

Tuesday, March 4, 2008

Alternate Comet Ruby On Rails

I happened to have a talk about the previous post to Raja. The implementation had its own problems and changes.

Firstly : The Service Layer of the app, the part which communicates with the queue, must be separated from the controller. We don't want the controller to have a heavy load. The service layer will not know the part of the application that communicates with it and the controller will not know who its contacts. This ensures that even if the service layer has implementation changes, it will not affect the controller thus making it entirely a different entity. I guess this kind of distinction will only come with age and experience. The Facade Pattern is the base.

Secondly: The application is better off with cache implementations. Mainly because the queues are designed to serve a purpose. Communication between apps. The Cache will also be maintained by a service layer and the access via the controller will either be fetched from the cache or if expired from the database. To have a uniformity between the kind of platforms accessing the application (say Adobe AIR and the browser accessing the Mongrel server), we could have an identifying parameter parameter passed along with the url which tells the controller the type of client and the resultant format of output (say XML/ JSON/RHTML).

I feel I have lifted myself a step now !! Thanks Raja .