Table of contents
1.
Introduction
2.
What is caching?
3.
Caching selects 
4.
Aliases
4.1.
Example
5.
Frequently asked questions
5.1.
What is web2py?
5.2.
web2py works on which framework?
5.3.
Who developed web2py?
5.4.
What is a loop?
5.5.
In which year web2py was firstly released?
6.
Conclusion
Last Updated: Aug 13, 2025
Easy

Caching selects and aliases in web2py

Author Ankit Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Do you remember when you planned to buy your laptop back in your teens? You must have been into checking all the specifications and configurations of your desired devices every now and then. One of these configurations to select the better device must be the cache memory. Today, the day has finally come when we will be learning what caching is and what we mean by caching selects and aliases in the famous python framework that is web2py.

Introductory image

What is caching?

Cache or the cache memory is nothing but temporary storage, which is easily and quickly accessible. The data inside the cache memory is stored just below the CPU, and the process of storing the data inside the cache memory is known as caching.

It provides so many advantages such as speeding up the processes, a better and fast user experience, and also it stores the data locally. Now the question arises what do we mean by caching selects in web2py.

RAM

Caching selects 

The cache argument for the select method also accepts a value of None by default. A tuple with the first element being the cache model (cache.ram, cache. disk, etc.) and the second being the expiration time in seconds should be used for caching.

We can see a controller caching a select on the previously defined db.log table in the example given below. The actual select method retrieves information from the back-end database just once every 60 seconds or so and then stores the outcome in memory. This controller merely gets the previous data from memory if the interval between calls is shorter than 60 seconds.

def cache_db_select():
    logs = db().select(db.log.ALL, cache=(cache.ram, 60))
    return dict(logs=logs)


The cacheable argument for the select method is optional and typically False. Although the resultant Rows are serialisable when cacheable=True, they do not include update record and delete record methods.

If we do not need these methods, we can speed up the select method using the cacheable attribute with the help of the following command.

rows = db(query).select(cacheable=True)


Only the database results, not the actual Rows object, are cached when the cache parameter is set but cacheable=False (the default). The entire Rows object is cached when the cache parameter is used in conjunction with cacheable=True, which leads to substantially faster caching.

rows = db(query).select(cache=(cache.ram, 3600), cacheable=True)


So with this, we saw how to cache selects in web2y. Let us now see what aliases in web2py are.

Aliases

As the name recommends, aliases mean a temporary name that we assign to the table or database column.

The ‘with_alias ’ method in web2py is used for making aliases of tables. This command is not restricted to making aliases of tables, and it can also be used to make aliases of fields and expressions.

Let us see an example of making aliases.

Example

>>> fid, mid = db.person.bulk_insert([dict(name='Mariah'), dict(name='Rakshim')])
>>> db.person.insert(name='Marcelo', father_id=fid, mother_id=mid)
>>> Father = db.person.with_alias('father')
>>> Mother = db.person.with_alias('mother')
>>> type(Father)
<class 'pydal.objects.Table'>
>>> str(Father)
'person AS father'
>>> rows = db().select(db.person.name, Father.name, Mother. name,
...                    left=(Father. on(Father.id == db.person.father_id),
...                          Mother.on(Mother.id == db.person.mother_id)))
>>> for row in rows:
...     print row.person.name, row.father.name, row.mother.name


Take note of how we have chosen to distinguish between:

"father id" is the name of the column in the "person" table; 

"father" is the alias we want to use for the table that is referenced by the aforementioned field; and

The variable "Father" is what web2py uses to refer to that alias.

Although there is a slight distinction, it is acceptable to refer to all three of them by the same name:

So the example signified the use of the ‘with_alias’ command in web2py. I hope you found this blog insightful.

You can also read about the memory hierarchy.

Let us now discuss some of the frequently asked questions on this topic.

Frequently asked questions

What is web2py?

web2py is an open-source web application framework that focuses on rapid development by emphasizing ease of use and productivity.

web2py works on which framework?

web2py is a very flexible language. Web2py supports all the control structures that python supports. Web2py works on the full-stack framework, which is very helpful in developing database-driven web applications. 

Who developed web2py?

Massimo Di Pierro developed web2py.

What is a loop?

Loop is just a way to simplify the task of repeating sets of instructions.

In which year web2py was firstly released?

In 2007 first version of web2py was released.

Conclusion

In this article, we extensively discussed web2py, which is an open-source language. Very fast, fluid in nature. We started with the introduction, and then we discussed how to cache select methods in web2py. It was then followed by learning to create aliases for the tables and expressions. In the end, we discussed a few frequently asked questions.

Now you must be curious after knowing about web2py and how simple it is to learn. You can visit Basics of Python with Data Structures and Algorithms and Free Python Foundation with DS and Algo and start your journey.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Thankyou image
Live masterclass