🍃 Debris
Install
pip install debris
Featuring
- Distribution network for data
- retreive data from different sources to distribute retrieval work loads
- simply: free up sql requests
- Schema driven cache routing
- Optimize your applications data gets/puts
- Reduce bottlenecks from addon services
- keep data integrity cross many services including
redis
,mongodb
,memcached
and more
- keep data integrity cross many services including
- Class instance continuity
- python classes constructed with the same arguments will not create new object but use the one already constructed
Usage
♊ Same-Same Instances
Traditionally in python these objects would not be the exact same on memory.
import debris
class User(object):
__metaclass__ = debris.Object
def __init__(self, id, **data):
self.name = data['name']
>>> User(1) is User(1)
True # w/out debris this would be False
>>> id(User(1)) == id(User(1))
True
>>> User(1).name = "Steve"
>>> User(1).name
"Steve"
🐙 Initializing
import debris
debris.config({
"services": {
"postgresql": {"url": "postgresql://..."}
},
"objects": {
"User": {
"get": [
{
"service": "postgresql",
"query": "select name, email from users where id=%(id)s limit 1;"
}
]
}
}
})
class User(object):
__metaclass__ = debris.Object
def __init__(self, id, **data):
self.id = id
self.name = data['name']
self.email = data['email']
>>> User(15).name
"John"
Constructing your objects typically is a query to the database. So lets establish this with debris. We configure debris to initiate postgresql
service so we can get our Users from the database. This, at first glance, may look unnecessary and not fixing any problem. So lets add some more services to see the full benefits.
... Need to add more content here. ...
Rules
-
No data, no instance when no data is found the requested object will not initialize and a
LookupError
will be raises. -
Same, Same instances with the same arguments will be considered equivelent
- the first initialized instance will be returned, therefore not initializing a new instance
🍬 Wrapping Web Requests
Decorate your web request handlers for quicker request times.
Use python decorators to stash your request results. This process can dramatically speed up your web server requests by storing the results for later use, instead of rendering them on the fly each request.
Supported Frameworks
Tornado Web
Wrappingimport tornado.ioloop
import tornado.web
import tornado.httpclient
import time
from debris.tornado import cached
class MainHandler(tornado.web.RequestHandler):
@cached("homepage")
def get(self):
# example request that takes some time()
time.sleep(5)
# finsih with anything: html, json, xml etc.
self.finish("<html>This is now prebuilt</html>")
application = tornado.web.Application([(r"/", MainHandler)])
if __name__ == '__main__':
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
Cool! Now run the server via python server.py
and curl some requests.
$ curl http://localhost:8888/ -w %{time_total}s
5.110s
$ curl http://localhost:8888/ -w %{time_total}s
0.098s
Your second request was cached in memory and was able to return the request results very quickly.
Roadmap
- Switch to a
Debris
asset class container for all cached data.- Set the
expiration
,callback
and more preferences - Expritation may be based on
eta
,date
orsize
- Set the
- Support for storing the cache results in other databases for data redundancy.
- Known already as
banks
inc. support formemcache
,redis
andmongodb
- Known already as
- Create methods to clear the stashed assets through request handlers and system events
- Metrics on cache size, requests and frequency
- Golden Rules what all assets will comply with before being cached