Search in sources :

Example 11 with Context

use of doitincloud.rdbcache.supports.Context in project rdbcache by rdbcache.

the class RdbcacheApis method delall_get.

/**
 * delall_get get single item
 *
 * To delete a key from redis and database based on the input key. No query string.
 * It returns immediately.
 *
 * @param request HttpServletRequest
 * @param key String, hash key
 * @param opt1 String, can be "sync" or "async" and table
 * @param opt2 String, can be "sync" or "async"  and table, but not the same as opt1
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/delall/{key}", "/rdbcache/v1/delall/{key}/{opt1}", "/rdbcache/v1/delall/{key}/{opt1}/{opt2}" }, method = RequestMethod.GET)
public ResponseEntity<?> delall_get(HttpServletRequest request, @PathVariable("key") String key, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2) {
    if (request.getParameterMap().size() != 0) {
        throw new BadRequestException("query string is not supported");
    }
    if (key.equals("*")) {
        throw new BadRequestException("no * allowed as key");
    }
    Context context = new Context();
    KvPairs pairs = new KvPairs(key);
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2);
    if (anyKey.getKeyInfo().getIsNew()) {
        throw new NotFoundException("key not found for " + key);
    }
    LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
    AppCtx.getAsyncOps().doDeleteFromRedisAndDbase(context, pairs, anyKey);
    return Response.send(context, pairs);
}
Also used : Context(doitincloud.rdbcache.supports.Context) AnyKey(doitincloud.rdbcache.supports.AnyKey) BadRequestException(doitincloud.commons.exceptions.BadRequestException) NotFoundException(doitincloud.commons.exceptions.NotFoundException) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 12 with Context

use of doitincloud.rdbcache.supports.Context in project rdbcache by rdbcache.

the class RdbcacheApis method getset_get.

/**
 * getset_get get single item
 *
 * To get current value of a key and update it to a new value based on the key and/or query string.
 * It finds the current value and returns immediately, and asynchronously updates to redis and database
 *
 * @param request HttpServletRequest
 * @param key String, hash key
 * @param value String, value
 * @param opt1 String, can be expire or table or "sync" or "async"
 * @param opt2 String, can be expire or table or "sync" or "async", but not otp1
 * @param opt3 String, can be expire or table or "sync" or "async", but not otp1 and opt2
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/getset/{key}/{value}", "/rdbcache/v1/getset/{key}/{value}/{opt1}", "/rdbcache/v1/getset/{key}/{value}/{opt1}/{opt2}", "/rdbcache/v1/getset/{key}/{value}/{opt1}/{opt2}/{opt3}" }, method = RequestMethod.GET)
public ResponseEntity<?> getset_get(HttpServletRequest request, @PathVariable("key") String key, @PathVariable("value") String value, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2, @PathVariable Optional<String> opt3) {
    Context context = new Context(true);
    KvPairs pairs = new KvPairs(key, value);
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2, opt3);
    LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
    KvPairs pairsClone = pairs.clone();
    KvPair pair = pairs.getPair();
    if (key.equals("*")) {
        if (!AppCtx.getDbaseRepo().find(context, pairs, anyKey)) {
            pair.clearData();
        }
        AppCtx.getAsyncOps().doSaveToRedis(context, pairsClone, anyKey);
    } else if (AppCtx.getRedisRepo().findAndSave(context, pairs, anyKey)) {
        AppCtx.getAsyncOps().doSaveToDbase(context, pairsClone, anyKey);
    } else {
        if (!AppCtx.getDbaseRepo().find(context, pairs, anyKey)) {
            pair.clearData();
        }
        AppCtx.getAsyncOps().doSaveToDbase(context, pairsClone, anyKey);
    }
    return Response.send(context, pairs);
}
Also used : Context(doitincloud.rdbcache.supports.Context) AnyKey(doitincloud.rdbcache.supports.AnyKey) KvPair(doitincloud.rdbcache.models.KvPair) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 13 with Context

use of doitincloud.rdbcache.supports.Context in project rdbcache by rdbcache.

the class RdbcacheApis method delkey_get.

/**
 * delkey_get get/delete single item
 *
 * To delete a key from redis based on the input key. No query string.
 * It returns immediately. It will not delete database entry.
 *
 * @param request HttpServletRequest
 * @param key String, hash key
 * @param opt1 String, can be "sync" or "async" and table
 * @param opt2 String, can be "sync" or "async"  and table, but not the same as opt1
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/delkey/{key}", "/rdbcache/v1/delkey/{key}/{opt1}", "/rdbcache/v1/delkey/{key}/{opt1}/{opt2}" }, method = { RequestMethod.GET, RequestMethod.DELETE })
public ResponseEntity<?> delkey_get(HttpServletRequest request, @PathVariable("key") String key, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2) {
    if (request.getParameterMap().size() != 0) {
        throw new BadRequestException("query string is not supported");
    }
    if (key.equals("*")) {
        throw new BadRequestException("no * allowed as key");
    }
    Context context = new Context();
    KvPairs pairs = new KvPairs(key);
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2);
    if (anyKey.getKeyInfo().getIsNew()) {
        throw new NotFoundException("key not found for " + key);
    }
    LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
    AppCtx.getAsyncOps().doDeleteFromRedis(context, pairs, anyKey);
    return Response.send(context, pairs);
}
Also used : Context(doitincloud.rdbcache.supports.Context) AnyKey(doitincloud.rdbcache.supports.AnyKey) BadRequestException(doitincloud.commons.exceptions.BadRequestException) NotFoundException(doitincloud.commons.exceptions.NotFoundException) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 14 with Context

use of doitincloud.rdbcache.supports.Context in project rdbcache by rdbcache.

the class RdbcacheApis method trace_get.

/**
 * trace_get get single item
 *
 * get error messages by trace id
 *
 * @param request HttpServletRequest
 * @param traceId the trace id return by API call
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/trace/{traceId}" }, method = RequestMethod.GET)
public ResponseEntity<?> trace_get(HttpServletRequest request, @PathVariable("traceId") String traceId) {
    if (request.getParameterMap().size() != 0) {
        throw new BadRequestException("query string is not supported");
    }
    if (traceId.equals("*")) {
        throw new BadRequestException("no * allowed as trace id");
    }
    if (request.getParameterMap().size() != 0) {
        throw new BadRequestException("no query string is allowed");
    }
    Context context = new Context(true);
    KvPairs pairs = new KvPairs();
    Request.process(context, request, pairs);
    LOGGER.trace("pairs(" + pairs.size() + "): " + pairs.printKey());
    KvPair pair = AppCtx.getKvPairRepo().findById(new KvIdType(traceId, "trace"));
    if (pair != null) {
        pairs.add(pair);
    }
    return Response.send(context, pairs);
}
Also used : Context(doitincloud.rdbcache.supports.Context) KvIdType(doitincloud.rdbcache.models.KvIdType) KvPair(doitincloud.rdbcache.models.KvPair) BadRequestException(doitincloud.commons.exceptions.BadRequestException) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 15 with Context

use of doitincloud.rdbcache.supports.Context in project rdbcache by rdbcache.

the class RdbcacheApis method flushcache_get.

/**
 * flushcache_get get operational to multiple items
 *
 * flush local cache
 *
 * @param request HttpServletRequest
 * @param opt optional, all, table, key and data
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/flush-cache", "/rdbcache/v1/flush-cache/{opt}" }, method = RequestMethod.GET)
public ResponseEntity<?> flushcache_get(HttpServletRequest request, @PathVariable Optional<String> opt) {
    if (request.getParameterMap().size() != 0) {
        throw new BadRequestException("query string is not supported");
    }
    Context context = new Context();
    Request.process(context, request);
    if (!opt.isPresent()) {
        AppCtx.getCacheOps().removeAllKeyAndData();
    } else {
        String option = opt.get();
        if (option.equals("all")) {
            AppCtx.getCacheOps().removeAll();
        } else if (option.equals("table")) {
            AppCtx.getCacheOps().removeAllTables();
        } else if (option.equals("key")) {
            AppCtx.getCacheOps().removeAllKeyInfo(null);
        } else if (option.equals("data")) {
            AppCtx.getCacheOps().removeAllData(null);
        } else if (option.equals("key-and-data")) {
            AppCtx.getCacheOps().removeAllKeyAndData();
        } else {
            throw new BadRequestException("unknown option: " + option);
        }
    }
    Map<String, Object> data = new LinkedHashMap<>();
    data.put("result", "DONE");
    return Response.send(context, data);
}
Also used : Context(doitincloud.rdbcache.supports.Context) BadRequestException(doitincloud.commons.exceptions.BadRequestException)

Aggregations

Context (doitincloud.rdbcache.supports.Context)33 KvPairs (doitincloud.rdbcache.supports.KvPairs)25 AnyKey (doitincloud.rdbcache.supports.AnyKey)23 KeyInfo (doitincloud.rdbcache.models.KeyInfo)17 BadRequestException (doitincloud.commons.exceptions.BadRequestException)15 KvPair (doitincloud.rdbcache.models.KvPair)15 Test (org.junit.Test)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 NotFoundException (doitincloud.commons.exceptions.NotFoundException)4 ServerErrorException (doitincloud.commons.exceptions.ServerErrorException)2 KvIdType (doitincloud.rdbcache.models.KvIdType)2 QueryInfo (doitincloud.rdbcache.queries.QueryInfo)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 MockServletContext (org.springframework.mock.web.MockServletContext)2 StopWatch (doitincloud.rdbcache.models.StopWatch)1 DbaseOps (doitincloud.rdbcache.services.DbaseOps)1 ExpireDbOps (doitincloud.rdbcache.supports.ExpireDbOps)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1