Skip to content

Instantly share code, notes, and snippets.

@fabware
Created April 20, 2011 08:21
Show Gist options
  • Save fabware/930708 to your computer and use it in GitHub Desktop.
Save fabware/930708 to your computer and use it in GitHub Desktop.
------------ model.py ----------------
meta = MetaData()
usersession_table = Table('kg_usersession', meta,
Column('session_id', String(32), primary_key=True),
Column('user_id', BigInteger, index=True, nullable=False),
Column('create_time', DateTime, index=True),
Column('expire_time', DateTime, index=True),
Column('site', String(10)),
mysql_engine='MyISAM'
)
class UserSession(object):
def __init__(self):
self.session_id = ''
self.user_id = None
self.create_time = None
self.expire_time = None
self.site = ''
usersession_shard_engines = {}
for i in range(settings.DB_USERSESSION_SHARD_MOD):
usersession_shard_engines[i] = create_engine(settings.DB_USERSESSION_URI+'_%d?charset=utf8'%i, encoding='utf-8', convert_unicode=True, pool_size=settings.DB_POOL_SIZE, pool_recycle=settings.DB_POOL_RECYCLE_TIMEOUT, echo=settings.DEBUG)
create_usersession_session = sessionmaker(class_=ShardedSession, expire_on_commit=False)
create_usersession_session.configure(shards=usersession_shard_engines)
def shard_value(value):
return int(value, 16) % settings.DB_USERSESSION_SHARD_MOD
def shard_chooser(mapper, instance, clause=None):
if instance:
return shard_value(instance.session_id)
def id_chooser(query, ident):
return [ i for i in range(settings.DB_USERSESSION_SHARD_MOD) ]
def query_chooser(query):
ids = []
for column, operator, value in get_query_comparisons(query):
if column.shares_lineage(usersession_table.c.session_id):
if operator == operators.eq:
if value:
ids.append(shard_value(value))
if operator == operators.in_op:
ids.extend([ shard_value(v) for v in value if v ])
if len(ids) == 0:
return [ i for i in range(settings.DB_USERSESSION_SHARD_MOD) ]
else:
return ids
create_usersession_session.configure(shard_chooser=shard_chooser,
id_chooser=id_chooser,
query_chooser=query_chooser)
mapper(UserSession, usersession_table)
--------------- bulk_delete.py ------------------
session = create_usersession_session()
session.query(UserSession).filter(UserSession.expire_time<=expire_time).delete();
session.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment