On Friday, March 18, 2016 at 1:19:40 PM UTC-4, Lisandro wrote:
>
>
> Now, the part that I'm worried about is this: *inside the same model, I
> store each record in a variable*, like this:
>
> db.define_table('days', Field('name'), Field('plural_name'))
>
> MONDAY = db.days[1]
> TUESDAY = db.days[2]
> WEDNESDAY = db.days[3]
> THURSDAY = db.days[4]
> FRIDAY = db.days[5]
> SATURDAY = db.days[6]
> SUNDAY = db.days[7]
>
> I use that to be able to reference each day from the app code, but I've
> realised that in this way, the "days" table won't be lazy, because it's
> called to get the record.
>
Forget about the laziness of the table here -- that's trivial compared to
the time it will take to run seven separate queries on every request.
Instead, you should retrieve all the rows in a single query and cache the
result for a really long time.
> I've thouth of an answer myself, but I'm not sure.
> One possible approach would be to define Storage objects instead of
> pointing to the records.
> Because I already know the data of the days table, I can replace the
> previous code with this:
>
> from gluon.tools import Storage
>
> MONDAY = Storage()
> MONDAY.id = 1
> MONDAY.name = 'monday'
> MONDAY.plural_name = 'mondays'
>
>
> *Would it be better in terms of performance?*
> Of course, I wouldn't have methods available at the row level, but I don't
> use them in this case.
>
Yes, something like this would be even better if you really don't have any
need for database functionality here. You could even put the data in a
module and import.
Anthony
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.