| 303 | class BaseStorage : |
| 304 | def __init__(self, pykotatool) : |
| 305 | """Opens the LDAP connection.""" |
| 306 | # raise PyKotaStorageError, "Sorry, the LDAP backend for PyKota is not yet implemented !" |
| 307 | self.closed = 1 |
| 308 | self.tool = pykotatool |
| 309 | self.usecache = pykotatool.config.getCaching() |
| 310 | if self.usecache : |
| 311 | self.tool.logdebug("Caching enabled.") |
| 312 | self.caches = { "USERS" : {}, "GROUPS" : {}, "PRINTERS" : {}, "USERPQUOTAS" : {}, "GROUPPQUOTAS" : {}, "JOBS" : {}, "LASTJOBS" : {} } |
| 313 | |
| 314 | def __del__(self) : |
| 315 | """Ensures that the database connection is closed.""" |
| 316 | self.close() |
| 317 | |
| 318 | def getFromCache(self, cachetype, key) : |
| 319 | """Tries to extract something from the cache.""" |
| 320 | if self.usecache : |
| 321 | entry = self.caches[cachetype].get(key) |
| 322 | if entry is not None : |
| 323 | self.tool.logdebug("Cache hit (%s->%s)" % (cachetype, key)) |
| 324 | else : |
| 325 | self.tool.logdebug("Cache miss (%s->%s)" % (cachetype, key)) |
| 326 | return entry |
| 327 | |
| 328 | def cacheEntry(self, cachetype, key, value) : |
| 329 | """Puts an entry in the cache.""" |
| 330 | if self.usecache : |
| 331 | self.caches[cachetype][key] = value |
| 332 | |
| 333 | def getUser(self, username) : |
| 334 | """Returns the user from cache.""" |
| 335 | user = self.getFromCache("USERS", username) |
| 336 | if user is None : |
| 337 | user = self.getUserFromBackend(username) |
| 338 | self.cacheEntry("USERS", username, user) |
| 339 | return user |
| 340 | |
| 341 | def getGroup(self, groupname) : |
| 342 | """Returns the group from cache.""" |
| 343 | group = self.getFromCache("GROUPS", groupname) |
| 344 | if group is None : |
| 345 | group = self.getGroupFromBackend(groupname) |
| 346 | self.cacheEntry("GROUPS", groupname, group) |
| 347 | return group |
| 348 | |
| 349 | def getPrinter(self, printername) : |
| 350 | """Returns the printer from cache.""" |
| 351 | printer = self.getFromCache("PRINTERS", printername) |
| 352 | if printer is None : |
| 353 | printer = self.getPrinterFromBackend(printername) |
| 354 | self.cacheEntry("PRINTERS", printername, printer) |
| 355 | return printer |
| 356 | |
| 357 | def getUserPQuota(self, user, printer) : |
| 358 | """Returns the user quota information from cache.""" |
| 359 | useratprinter = "%s@%s" % (user.Name, printer.Name) |
| 360 | upquota = self.getFromCache("USERPQUOTAS", useratprinter) |
| 361 | if upquota is None : |
| 362 | upquota = self.getUserPQuotaFromBackend(user, printer) |
| 363 | self.cacheEntry("USERPQUOTAS", useratprinter, upquota) |
| 364 | return upquota |
| 365 | |
| 366 | def getGroupPQuota(self, group, printer) : |
| 367 | """Returns the group quota information from cache.""" |
| 368 | groupatprinter = "%s@%s" % (group.Name, printer.Name) |
| 369 | gpquota = self.getFromCache("GROUPPQUOTAS", groupatprinter) |
| 370 | if gpquota is None : |
| 371 | gpquota = self.getGroupPQuotaFromBackend(group, printer) |
| 372 | self.cacheEntry("GROUPPQUOTAS", groupatprinter, gpquota) |
| 373 | return gpquota |
| 374 | |
| 375 | def getPrinterLastJob(self, printer) : |
| 376 | """Extracts last job information for a given printer from cache.""" |
| 377 | lastjob = self.getFromCache("LASTJOBS", printer.Name) |
| 378 | if lastjob is None : |
| 379 | lastjob = self.getPrinterLastJobFromBackend(printer) |
| 380 | self.cacheEntry("LASTJOBS", printer.Name, lastjob) |
| 381 | return lastjob |
| 382 | |