1 /*******************************************************************************
2 * Copyright 2016-2017 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
13 ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper.dao;
16 import java.util.ArrayList;
17 import java.util.HashSet;
18 import java.util.List;
21 import org.onap.msb.apiroute.wrapper.util.JedisUtil;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import redis.clients.jedis.Jedis;
26 import redis.clients.jedis.ScanParams;
27 import redis.clients.jedis.ScanResult;
29 public class RedisAccessWrapper {
30 private static final Logger LOGGER = LoggerFactory.getLogger(RedisAccessWrapper.class);
31 // An iteration starts when the cursor is set to 0
32 private static final String REDIS_SCAN_POINTER_NEW_ITERATION = "0";
33 // An iteration terminated when the cursor returned by the server is 0
34 private static final String REDIS_SCAN_POINTER_ITERATION_END = "0";
35 private static final int REDIS_SCAN_COUNT = 50;
38 public static void save(String key, String value) throws Exception {
41 jedis = JedisUtil.borrowJedisInstance();
42 jedis.set(key, value);
44 JedisUtil.returnJedisInstance(jedis);
48 public static String query(String key) throws Exception {
52 jedis = JedisUtil.borrowJedisInstance();
53 value = jedis.get(key);
55 JedisUtil.returnJedisInstance(jedis);
60 public static long delete(String key) throws Exception {
64 jedis = JedisUtil.borrowJedisInstance();
65 reply = jedis.del(key);
67 JedisUtil.returnJedisInstance(jedis);
72 public static boolean isExist(String key) throws Exception {
73 boolean isExist = false;
76 jedis = JedisUtil.borrowJedisInstance();
77 isExist = jedis.exists(key);
79 JedisUtil.returnJedisInstance(jedis);
84 public static List<String> queryMultiKeys(String keyPattern) throws Exception {
85 Set<String> keySet = filterKeys(keyPattern);
86 List<String> valueList = new ArrayList<>();
89 jedis = JedisUtil.borrowJedisInstance();
90 for (String key : keySet) {
91 String value = jedis.get(key);
92 if (value != null && !"".equals(value)) {
97 JedisUtil.returnJedisInstance(jedis);
102 public static long deleteMultiKeys(String keyPattern) throws Exception {
103 Set<String> keySet = filterKeys(keyPattern);
107 jedis = JedisUtil.borrowJedisInstance();
108 for (String key : keySet) {
109 long reply = jedis.del(key);
110 replySum = replySum + reply;
113 JedisUtil.returnJedisInstance(jedis);
119 * filter the keys according to the given pattern using "scan" instead of using "keys",
120 * incrementally iterate the keys space
122 * @param pattern the input filter pattern
123 * @return the matched keys set
125 public static Set<String> filterKeys(String pattern) throws Exception {
126 long start = System.currentTimeMillis();
128 Set<String> filteredKeys = new HashSet<>();
129 ScanParams scanParams = new ScanParams();
130 scanParams.match(pattern);
131 scanParams.count(REDIS_SCAN_COUNT);
133 jedis = JedisUtil.borrowJedisInstance();
134 ScanResult<String> scanResult = jedis.scan(REDIS_SCAN_POINTER_NEW_ITERATION, scanParams);
135 filteredKeys.addAll(scanResult.getResult());
136 while (!scanResult.getStringCursor().equals(REDIS_SCAN_POINTER_ITERATION_END)) {
137 scanResult = jedis.scan(scanResult.getStringCursor(), scanParams);
138 filteredKeys.addAll(scanResult.getResult());
141 JedisUtil.returnJedisInstance(jedis);
143 long end = System.currentTimeMillis();
144 long costTime = end - start;
145 LOGGER.info("filterKeys " + pattern + " count:" + filteredKeys.size() + " cost: " + costTime);