পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Monday, March 13, 2017

TurboGears – Caching

In order to enhance the performance of a web application, especially if it is involved in lengthy operations, caching techniques are used. TurboGears provides two types of caching techniques −
Whole-page Caching

It works at the HTTP protocol level to avoid entire requests to the server by having either the user’s browser, or an intermediate proxy server (such as Squid) intercept the request and return a cached copy of the file.
Application-level Caching
This works within the application server to cache computed values, often the results of complex database queries, so that future requests can avoid needing to re-calculate the values. For web applications, application-level caching provides a flexible way to cache the results of complex queries so that the total load of a given controller method can be reduced to a few user-specific or case-specific queries and the rendering overhead of a template.

Application-level Caching

As mentioned earlier, ‘quickstarted’ TurboGears project is configured to enable Beaker package for caching support. Beaker supports the following back-ends used for cache storage −
  • memory − Used for per-process storage. It is extremely fast.
  • filesystem − per-process storage as well as multi-process.
  • DBM database − per-process, multi-process, fairly fast.
  • SQLAlchemy database − per-database-server storage. Slower compared to the above given options.
  • Memcached − multi-server memory based cache.

Controller Caching

For quick controller caching, a cached() decorator is available. The entire controller body is cached depending on various parameters of request. The definition of tg.decorators.cached() decorator is as follows
tg.decorators.cached(key, expire, type, 
   query-args, cache_headers, invalidate_on_startup, cache_response)
The description of parameters is as follows −
S.No Parameters & Description
1 key
Specifies the controller parameters used to generate the cache key.
2 expire
Time in seconds before cache expires, Defaults to “never”.
3 Type
dbm, memory, file, memcached, or None.
4 cache_headers
A tuple of header names indicating response headers.
5 invalidate_on_startup
If True, cache is invalidated each time application starts or is restarted.
6 cache_response
response should be cached or not, defaults to True.
Following is an example of controller caching −
@cached(expire = 100, type = 'memory')
@expose()
def simple(self):
   return "This is a cached controller!"

Template Level Caching

The Genshi template engine retrieves template from a cache if its contents have not changed. The default size of this cache is 25. By default, automatic reloading of templates is true. In order to improve performance, the following settings can be made in app_cfg.py
[app:main]
genshi.max_cache_size = 100
auto_reload_templates = false
To cache a template, you just have to return the tg_cache option from the controller that renders the cached template.
The tg_cache is a dictionary that accepts the following keys −
  • key − The cache key. Default: None.
  • expire − how long the cache must stay alive. Default: never expires
  • type − memory, dbm, memcached. Default: dbm.
The following example illustrates template caching −
@expose(hello.templates.user')
def user(self, username):
   return dict(user = username, tg_cache = dict(key = user, expire = 900))

No comments:

Post a Comment