def delete(fid): """ 删除文件 """ if type(fid) is not ObjectId: fid = ObjectId(fid) gfs = get_context().get_gfs() gfs.delete(fid)
def exists(fid): """ 判断文件是否存在 """ if type(fid) is not ObjectId: fid = ObjectId(fid) gfs = get_context().get_gfs() return gfs.exists(fid)
def delete(*keys): """ 删除缓存 """ context = get_context() for k in keys: context.get_cache(k).delete(k)
def delete_by_filename(filename): """ 相同文件名可能保存了多个副本,统统删除。 """ gfs = get_context().get_gfs() while True: try: fid = gfs.get_version(filename = filename)._id gfs.delete(fid) except NoFile: break
def delete(*keys): """ 删除缓存 """ context = get_context() for k in keys: try: context.get_cache(k).delete(k) except: print >> stderr, "delete key {0} from cache ERROR, has cleared".format( key)
def get(fid): """ 获取文件内容和元数据 参数: fid 文件唯一编号 返回: (data, meta) 文件内容(字节数组), 元属性字典 """ if type(fid) is not ObjectId: fid = ObjectId(fid) gfs = get_context().get_gfs() with gfs.get(fid) as out: data, meta = out.read(), mongo_conv(out._file) if meta.get("gzip"): data = decompress(data) return data, meta
def op(key, method, *args, **kwargs): """ 缓存操作 参数: expire_seconds 设置过期秒数,0 表示移除过期设置。 """ expire_seconds = kwargs.get("expire_seconds") cache = get_context().get_cache(key) if expire_seconds is not None: kwargs.pop("expire_seconds") pipe = cache.pipeline() method(pipe, key, *args, **kwargs) expire_seconds > 0 and pipe.expire(key, expire_seconds) or pipe.persist(key) return pipe.execute()[0] else: return method(cache, key, *args, **kwargs)
def put(filename, data, gzip = False, **kwargs): """ 将字节数组作为文件内容保存到 GFS 参数: filename 文件识别名 data 文件内容 gzip 是否压缩 kwargs 附加元属性 返回: 文件唯一编号 """ kwargs.update(dict(filename = filename)) if gzip: kwargs["gzip"] = True data = compress(data) gfs = get_context().get_gfs() return gfs.put(data, **kwargs)
def op(key, method, *args, **kwargs): """ 缓存操作 参数: expire_seconds 设置过期秒数,0 表示移除过期设置。 """ expire_seconds = kwargs.get("expire_seconds") cache = get_context().get_cache(key) if expire_seconds is not None: kwargs.pop("expire_seconds") pipe = cache.pipeline() method(pipe, key, *args, **kwargs) expire_seconds > 0 and pipe.expire( key, expire_seconds) or pipe.persist(key) return pipe.execute()[0] else: return method(cache, key, *args, **kwargs)
def set(key, value, expire=None, new=False, mode=STRING): """ 设置缓存 参数: key value expire 过期时间(秒) new 是否删除已有的同名 KV mode 数据类型 示例: set("a", range(10), mode = SET) set("b", zip(range(11), [float(x) / 10 for x in range(10)]), mode = SORTEDSET) set("c", dict(a = 1, b = 2), expire = 60, mode = HASH) """ cache = get_context().get_cache(key) pipe = cache.pipeline() if new or (expire is not None) or (mode in ( LIST, SET, SORTEDSET)) else cache method = { STRING: pipe.set, HASH: pipe.hmset, LIST: pipe.rpush, SET: pipe.sadd, SORTEDSET: pipe.zadd }.get(mode) if new: pipe.delete(key) if mode in (LIST, SET, SORTEDSET): for v in value: # 2.2 不支持 multi... method(key, *v) if isinstance(v, (tuple, list)) else method(key, v) else: method(key, value) if expire is not None: pipe.expire(key, expire) if isinstance(pipe, Pipeline): pipe.execute()
def set(key, value, expire = None, new = False, mode = STRING): """ 设置缓存 参数: key value expire 过期时间(秒) new 是否删除已有的同名 KV mode 数据类型 示例: set("a", range(10), mode = SET) set("b", zip(range(10), [float(x) / 10 for x in range(10)]), mode = SORTEDSET) set("c", dict(a = 1, b = 2), expire = 60, mode = HASH) """ cache = get_context().get_cache(key) pipe = cache.pipeline() if new or (expire is not None) or (mode in (LIST, SET, SORTEDSET)) else cache method = { STRING: pipe.set, HASH: pipe.hmset, LIST: pipe.rpush, SET: pipe.sadd, SORTEDSET: pipe.zadd }.get(mode) if new: pipe.delete(key) if mode in (LIST, SET, SORTEDSET): for v in value: # 2.2 不支持 multi... method(key, *v) if isinstance(v, (tuple, list)) else method(key, v) else: method(key, value) if expire is not None: pipe.expire(key, expire) if isinstance(pipe, Pipeline): pipe.execute()
def exists_by_filename(filename): """ 删除文件 """ gfs = get_context().get_gfs() return gfs.exists(filename = filename)
def async_send_pack(cls, key, cmd, data, channel = None): """ 将数据发送到队列,以实现异步处理。 """ cls.send_pack(get_context().get_mq(key), channel or __conf__.SERVICE_CHANNEL, cmd, data)