Пример #1
0
def kv_put(key, value, overwrite=False):
    """PUTs a key-value pair for `key` and `value`

    `overwrite` == True : overwrites if `key` already exists
    `overwrite` == False : raises Exception if `key` already exists
    """
    KV = get_kv_storage_model()
    kv_obj = None
    if KV:
        if overwrite:
            # attempt to retrieve an existing
            kv_obj = _get_kv_obj(key)
            if kv_obj:
                # overwrite the value
                kv_obj.value = value
                kv_obj.save()

        if not kv_obj:
            # overwrite == False or does not already exist
            kv_obj = KV.objects.create(
                key=key,
                value=value
            )

        if kv_obj:
            # update the cache if value was successfully written
            c = KVStorageCache(prekey=key)
            c.cache_store(value)
    return kv_obj
Пример #2
0
def kv_get(key, cache_only=False):
    """GETs the value of `key` from key-value storage

    `cache_only` == True : skips lookup in db, returns the cached value or None
    """
    c = KVStorageCache(prekey=key)
    value = c.get()
    if value is None and not cache_only:
        kv_obj = _get_kv_obj(key)
        if kv_obj:
            value = kv_obj.value
            c.cache_store(value)
    return value