# use this to get the count cached
def get_tbbooks_count(exp=3600):
      cache.ram('tbbooks',lambda: db(db.tbbooks.id == 1).count(),exp)


# use this to create new tbbooks and update count
def increment_tbbooks_count(form=None):
      cache.ram.increment('tbbooks')
form = crud.create(db.tbbooks,onaccept =increment_tbbooks_count)

# use this to edit tbbooks to if they get deleted, count if decresed
def decrement_tbbooks_count(form=None):
      cache.ram.increment('tbbooks',-1)
form = crud.update(db.tbbooks,record,ondelete =
decrement_tbbooks_count)


On May 27, 3:27 am, scausten <[email protected]> wrote:
> Great thanks! Any other ideas from anyone to speed this up further?
>
> On May 27, 3:25 am, Mr admin <[email protected]> wrote:
>
> > OK here's an example of counting matching records with web2py syntax.
>
> > SQL version
> >        select count(*) from tbbooks where id = 1 group by id;
>
> > Note!  if you don't add the GROUP BY clause,  you'll get a count
> > of 1 record... even if there a NO MATCHES.... probably NOT what you intended
> > to do.
> > The GROUP BY clause give you an "Empty Set" for non-matching search results
>
> > web2py version
> >      matches = db(db.tbbooks.id == 1).count()
>
> > On Wed, May 26, 2010 at 2:44 PM, scausten <[email protected]> wrote:
> > > I'm adding files content to my database with the following code being
> > > called onvalidation when the form is submitted. It really simply
> > > generates a 6-digit alphanumeric code as an identifier for the file:
>
> > > codes = db().select(db.products.code)   # Grabs all the existing codes
> > > from the database
> > > while not form.vars.code:
> > >    code = "".join([random.sample(string.ascii_lowercase
> > > +string.digits, 1)[0] for i in range(6)])  # Creates a code
> > >    if not code in codes:  # If its unique...
> > >        form.vars.code = code   # ...add it into the form vars
>
> > > It works fine at the moment, but I'm hoping to have several hundred
> > > thousand files potentially, and I'm worried that the database call to
> > > pull all existing codes will become a serious bottleneck. The code
> > > needs to be random and not sequential.
>
> > > Does anyone have any ideas on how I can do this more elegantly?

Reply via email to