-
Notifications
You must be signed in to change notification settings - Fork 364
FireflyAPI dbentrust API
Package dbentrust is mainly dealing with database and memcached storage, and encapsulates inside mapping memcached data to python objects. It could operate stored data in memcached by manipulating python object, and make sure the data consistency of objects, as long as instantiating homonymous memobject objects in disparate process. It can also synchronize object’s data to database. There is no need to write any sql sentence.
Database connection pool
Initialize database connection pool
type method
//cwd firefly/dbentrust/dbpool.py
Prams initPool(**kw)
**kw: key-word parameter, is a dictionary; key: value is the corresponding values filled in configuration file config.json respectively:
"host":"localhost",
"user":"root",
"passwd":"111",
"port":3306,
"db":"test",
"charset":"utf8"
Use to initialize database connection pool
Obtain a database connection
type method
//cwd firefly/dbentrust/dbpool.py
Params connecton(self)
Memcached client connection class, allocates different namespace by adding prefix class mode.
Connect memcached service
type method
//cwd firefly/dbentrust/memclient.py
Params connect(urls,hostname)
urls: the required ip and port number of connecting memcached service
Hostname, hostname that it connected to
The above two parameter’s value are the corresponding value filled in the configuration file config.json:
"urls":["127.0.0.1:11211"],
"hostname":"anheisg"
Use to connect memcached service
Regenerate key
type method
//cwd firefly/dbentrust/memclient.py
Params produceKey(self,keyname)
key: key name when it accesses value, generate uniform rules new key name according to this key name
Key must be str type , otherwise it will return type error
Obtain the corresponding value of key
type method
//cwd firefly/dbentrust/memclient.py
Prams get(key)
Obtain multiple key values at one time, faster than for-loop calling get method
@param key**: list(str)** : key’s list
type method
//cwd firefly/dbentrust/memclient.py
Params get_multi(keys)
keys is list type, keys = [key1,key2,...keyn],key1,key2 are str type
Set the keyname value as ‘value’
Type method
//cwd firefly/dbentrust/memclient.py
Params set(keyname, value)
Return True on success, 0 on failure.
Set multiple Key-Value Pair
type method
//cwd firefly/dbentrust/memclient.py
Params set_multi(mapping)
mapping is dic type, mapping = {key1:balue1,key2:value2}, return True on success, 0 on failure.
addself
type method
//cwd firefly/dbentrust/memclient.py
Params incr(key, delta)
Key: key name
Delta: value that added
Add delta to key’s value and return the value that added delta
Delete the assigned key
type method
//cwd firefly/dbentrust/memclient.py
Params delete(key)
key: key name
Delete key and its corresponding value, and return value is 1
delete multiple key at one time
type method
//cwd firefly/dbentrust/memclient.py
Params delete_multi(keys)
keys is list type, keys = [key1,key2,...keyn] key1,key2 is str type
Wipe all data (use with caution)
type method
//cwd firefly/dbentrust/memclient.py
Params flush_all(self)
Mapping class of memcached data to python objects, it realizes the customizing of memcached data format by inheriting this class.
Regenerate key
type method
//cwd firefly/dbentrust/memobject.py
Params produceKey(self,keyname)
key: key name when it accesses value, generate uniform rules new key name according to this key name
Key must be str type , otherwise it will return type error
Check whether the object is locked
type method
//cwd firefly/dbentrust/memobject.py
Params lock(self)
Return object’s state: 1means object is locked and cannot be modified, 0 means it can be modified.
Lock the object
type method
//cwd firefly/dbentrust/memobject.py
Params lock(self)
Modify the object’s lock status to 1
Release the lock
type method
//cwd firefly/dbentrust/memobject.py
Params release(self)
Modify the object’s lock state to 1
Obtain key’s corresponding value
type method
//cwd firefly/dbentrust/memobject.py
Params get(key)
Obtain multiple key values at one time, faster than for-loop calling get method
type method
//cwd firefly/dbentrust/memobject.py
Params get_multi(keys)
keys is list type, keys = [key1,key2,...keyn],key1,key2 are str type
Set key’s value as values
type method
//cwd firefly/dbentrust/memobject.py
Params update(key, values)
Modify multiple Key-Value Pair’s value at the same time
type method
//cwd firefly/dbentrust/memobject.py
Params update_multi(mapping)
mapping is dict type, mapping = {key1:balue1,key2:value2}, return True on success, 0 or false on failure.
Delete all data in this class
type method
//cwd firefly/dbentrust/memobject.py
Params mdelete(self)
addself
type method
//cwd firefly/dbentrust/memclient.py
Params incr(key, delta)
Key: key name
Delta: value that added
Add delta to key’s value and return the value that added delta
Insert all data included in this class
type method
//cwd firefly/dbentrust/memobject.py
Params insert()
Direct mapping class of database to memcached, it can modify data in database in deferred and asynchronous way, and reduces pressure on database. It has customized expiration time and data in memcached will be automatically wiped after this period. Here the definition of expiration is the interval time between two times the object be accessed. This class realizes itself by inheriting MemObject.
Set key’s value as values
type method
//cwd firefly/dbentrust/memobject.py
Params update(key, values)
modify multiple Key-Value Pair’s value at the same time
type method
//cwd firefly/dbentrust/memobject.py
Params update_multi(mapping)
mapping is dict type, mapping = {key1:balue1,key2:value2}, return True on success, 0 or false on failure.
Obtain key’s corresponding value
type method
//cwd firefly/dbentrust/memobject.py
Params get(key)
Obtain multiple key values at one time, faster than for-loop calling get method
type method
//cwd firefly/dbentrust/memobject.py
Params get_multi(keys)
keys is list type, keys = [key1,key2,...keyn],key1,key2 are str type
Delete object, here we just modify the status of date to “deleted”
type method
//cwd firefly/dbentrust/ mmode.py
Params delete(self)
Clean up objects, first synchronize data to database, and then actually clean the data in memecached.
type method
//cwd firefly/dbentrust/ mmode.py
Params mdelete(self)
Check whether the object is valid, return True on valid, false on invalid.
type method
//cwd firefly/dbentrust/ mmode.py
Params IsEffective(self)
Synchronize data to database
type method
//cwd firefly/dbentrust/ mmode.py
Params syncDB(self)
Synchronize data to database and check whether data is timeout, delete data in memcached if data is timeout
type method
//cwd firefly/dbentrust/ mmode.py
Params checkSync(self,timeout=TIMEOUT)
Foreign key management
mmode manager, can obtain a mmode instance according to primary key. A MAdmin management corresponds to a particular table in database and inherits the MemObject class. Its instantiation way is shown below:
ma = MAdmin("tb_user",'id',fk = 'group',incrkey='id')
user2 = ma.getObj(2)
user2.update('name','test')
it shows that tb_user is the corresponding table of database, it can obtain mmode object that id “2”record corresponds via ma.getObj(2).
user2.update('name','test'): modify the name field that this record corresponds to ‘test’.
Write all data in memcached.
type method
//cwd firefly/dbentrust/ mmode.py
Params insert(self)
Write all messages of table in database that this manager corresponds into memcached.
type method
//cwd firefly/dbentrust/ mmode.py
Params load(self)
Obtain Madmin’s relevant information
type method
//cwd firefly/dbentrust/ mmode.py
Params madmininfo(self)
Search the matching object (an inefficient way, not recommended for using)
type method
//cwd firefly/dbentrust/ mmode.py
Params mfilter(self,kw)
Obtain primary key list according to foreign key
type method
//cwd firefly/dbentrust/ mmode.py
Params getAllPkByFk(self,fk)
fk: foreign key value
Search all eligible data in this list according to foreign key value, return primary key value’s list of this data.
Obtain corresponding recorded mmode object according to primary key.
type method
//cwd firefly/dbentrust/ mmode.py
Params getObj(pk)
pk: primary key’s value, return mmode object instance of data that this primary key corresponds, type is instance
Obtain corresponding recorded mmode object’s field data according to primary key.
type method
//cwd firefly/dbentrust/ mmode.py
Params getObjData(self,pk)
pk: primary key’s value, return data that this primary key corresponds, type is dict
Obtain corresponding recorded mmode object’s list according to primary key.
type method
//cwd firefly/dbentrust/ mmode.py
Params getObjList(self,pklist)
Pklist: primary key list, return list of mmode object instance that primary key corresponds’ data according to primary key in list. ([instance,instance,instance])
Delete corresponding mmode messages according to primary key.
type method
//cwd firefly/dbentrust/ mmode.py
Params deleteMode(self,pk)
pk: primary key’s value
Here we just modify the status of date to “deleted”
checkAll
Check the controlled object’s messages and synchronize them to database, and process time-out objects
type method
//cwd firefly/dbentrust/ mmode.py
check()
Delete all foreign keys
type method
//cwd firefly/dbentrust/ mmode.py
Params deleteAllFk(self)
It actually deletes data from memcached.
Create a new object and set a corresponding record in database in a not simultaneous and real-time way. It could actually synchronize to database after checkAll operation is executed.
type method
//cwd firefly/dbentrust/ mmode.py
Params new(self,data)
madmin object manager, which actually is supervisor of whole database table records, yet the corresponding table Madmin must be registered here.
Register Madmin manager
type method
//cwd firefly/dbentrust/ madminanager.py
Params registe(self,admin)
Admin: data model object instance
Add instance of data object Madmin to its singleton MadminManager
Cancel the management of madmin object
type method
//cwd firefly/dbentrust/ madminanager.py
Params dropAdmin(self,adminname)
adminname: name of data model object instance
Delete data object Madmin’s instance from Madmin singleton MadminManager according to instance adminname.
Obtain registered madmin object according to table name
type method
//cwd firefly/dbentrust/ madminanager.py
Params getAdmin(self,adminname)
Adminnmae: name of data model object instance
Obtain data object Madmin’s instance from Madmin singleton MadminManager according to instance adminname.
Synchronize all registered madmin responding records in MadminManager to database, and process time-out objects
type method
//cwd firefly/dbentrust/ madminanager.py
Params checkAdmins(self)