Thursday, September 4, 2008

Some information about session and caching in Rails!!

What is Session?
A session is a method of storing object data temporarily between different requests. A session makes it easy to work with logged-in users because you do not have to authenticate them each time they access a protected area of the application. Instead, you can authenticate them once and store their data in a session. A session is merely a container for storing a user’s current state in a web application. Unlike a cookie, session information is stored on the server instead of the client’s computer. Rails offer 4 different ways to store data in session.
1. Memory Store
Rails keeps your session data in server memory
2. ActiveRecord Store
Rails keeps session data in session table in the database
3. Drb Store
This store uses distributed Ruby to store a user’s session data. The performance is great, but it requires a bit more setup than the other stores.
4. PStore
This is the default solution that is used in Rails. Using PStore, your session data is stored in small temporary files on your hard drive. These files are usually located in the tmp/sessions folder for the Rails app. The main downside of using the PStore is that you will have to do some session-pruning periodically because performance decreases as the number of sessions stored increases.

What is Caching?
Caching is the process of saving a copy of the results of a web request on the server or a local machine for subsequent requests
Types:
1. Page:
Whole static page is cached. Not suitable for dynamic pages like greeting messages, etc.
2. Action:
This is useful for authenticated users. It helps to keep the dynamic content on the sidebar.
3. Fragment:
You need to cache the portion of a page like header and footer.

Four places to keep the cache:
1. MemoryStore:
This store keeps the fragments in your application's memory, which can potentially take up a lot of memory on your server. It is used by default, but it is hard to manage and scale if your application becomes popular.
2. FileStore: This store keeps the fragments on the hard disk instead of in memory. It works well if you have a lot of file storage and have outgrown the MemoryStore.
3. DRbStore: This store keeps the fragments in the memory on a separate shared Drb server (Drb stands for Distributed Ruby). It keeps only one cache around for all processes. This is a complex solution because it involves setting up a secondary server.
4. MemCacheStore: Similar to DRbStore in that it stores your caches on a separate server, but uses the MemCache library. It also requires you to install the ruby-memcache library.

1 comment:

Jagan Reddy said...

nice article for learners.

thanks for posting