一个简单的概率计数器。
probcount的Python项目详细描述
一种简单有效的概率计数器,适用于速率限制和有效计数,无需将每个动作上传到远程数据库。
用法
指令
fromprobcountimportProbCounterfromfunctoolsimportpartialpc=ProbCounter()defold_increase_function(counter_name,value):# run the real call to the DB increasing the counter# for a redis# return redis.Redis().incrby(name=counter_name, amount=value)# or a MongoDB# return (pymongo.MongoClient().database_name.collection_name.find_one_and_update({"_id": counter_name},{"$inc":{"value":value}},{"value":1}).get("value",0)+value)passdefold_get_function(counter_name):# run the real call to the DB increasing the counter# for a redis# redis.Redis().get(name=counter_name)# or a MongoDB# pymongo.MongoClient().database_name.collection_name.find_one({"_id": counter_name}, {"value": 1}).valuepasspc.create("name_of_counter",partial(old_increase_function,"name_of_counter"),partial(old_get_function,"name_of_counter"))pc.inc("name_of_counter",42.0)pc.get("name_of_counter")
支持的Python版本
python项目模板支持以下现成版本:
- cpython 2.6、2.7、3.3、3.4、3.5、3.6、3.7
- Pypy 1.9