Changeset 261 for trunk/web/webcommon.py


Ignore:
Timestamp:
Jan 24, 2008, 9:34:53 PM (16 years ago)
Author:
ecprice
Message:

Use cached ACLs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/web/webcommon.py

    r236 r261  
    11"""Exceptions for the web interface."""
    22
    3 from sipb_xen_database import Machine
     3from sipb_xen_database import Machine, MachineAccess
    44
    55class MyException(Exception):
     
    2525import controls
    2626
     27def cachedproperty(func):
     28    name = '__cache_' + func.__name__ + '_' + str(id(func))
     29    def getter(self):
     30        try:
     31            return getattr(self, name)
     32        except AttributeError:
     33            value = func(self)
     34            setattr(self, name, value)
     35            return value
     36    return property(getter)
     37
    2738class Global(object):
    2839    """Global state of the system, to avoid duplicate remctls to get state"""
    2940    def __init__(self, user):
    3041        self.user = user
    31 
    32     def __get_uptimes(self):
    33         if not hasattr(self, '_uptimes'):
    34             self._uptimes = controls.getUptimes(Machine.select())
    35         return self._uptimes
    36     uptimes = property(__get_uptimes)
     42   
     43    machines = cachedproperty(lambda self:
     44                             [ma.machine for ma in
     45                              MachineAccess.select_by(user=self.user)])
     46    uptimes = cachedproperty(lambda self:
     47                             controls.getUptimes(self.machines))
    3748
    3849    def clear(self):
    3950        """Clear the state so future accesses reload it."""
    40         for attr in ('_uptimes', ):
    41             if hasattr(self, attr):
     51        for attr in self.__dict__:
     52            if attr.startswith('__cache_'):
    4253                delattr(self, attr)
    4354
Note: See TracChangeset for help on using the changeset viewer.