1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
| package com.example.redisbloom;
import org.springframework.data.redis.connection.ReturnType; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component;
import java.util.Collections; import java.util.List;
@Component public class RedisBloomFilterTool {
private final StringRedisTemplate redisTemplate;
public RedisBloomFilterTool(StringRedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; }
public void reserve(String key, double errorRate, long capacity) { redisTemplate.execute((RedisCallback<String>) connection -> connection.scriptingCommands().eval( ("return redis.call('BF.RESERVE', KEYS[1], " + errorRate + ", " + capacity + ")").getBytes(), ReturnType.STATUS, 1, key.getBytes() ) ); }
public boolean add(String key, String value) { return Boolean.TRUE.equals(redisTemplate.execute((RedisCallback<Boolean>) connection -> connection.scriptingCommands().eval( ("return redis.call('BF.ADD', KEYS[1], ARGV[1])").getBytes(), ReturnType.BOOLEAN, 1, key.getBytes(), value.getBytes() ) )); }
public boolean exists(String key, String value) { return Boolean.TRUE.equals(redisTemplate.execute((RedisCallback<Boolean>) connection -> connection.scriptingCommands().eval( ("return redis.call('BF.EXISTS', KEYS[1], ARGV[1])").getBytes(), ReturnType.BOOLEAN, 1, key.getBytes(), value.getBytes() ) )); }
public List<Boolean> addBatch(String key, String... items) { if (items == null || items.length == 0) { return Collections.emptyList(); }
byte[][] keysAndArgs = new byte[1 + items.length][]; keysAndArgs[0] = key.getBytes(); for (int i = 0; i < items.length; i++) { keysAndArgs[i + 1] = items[i].getBytes(); }
return redisTemplate.execute((RedisCallback<List<Boolean>>) connection -> connection.scriptingCommands().eval( ("return redis.call('BF.MADD', KEYS[1], unpack(ARGV))").getBytes(), ReturnType.MULTI, 1, keysAndArgs ) ); }
public List<Boolean> insert(String key, long capacity, double errorRate, String... items) {
if (items == null || items.length == 0) { return Collections.emptyList(); }
byte[][] keysAndArgs = new byte[6 + items.length][]; keysAndArgs[0] = key.getBytes(); keysAndArgs[1] = "CAPACITY".getBytes(); keysAndArgs[2] = String.valueOf(capacity).getBytes(); keysAndArgs[3] = "ERROR".getBytes(); keysAndArgs[4] = String.valueOf(errorRate).getBytes(); keysAndArgs[5] = "ITEMS".getBytes(); for (int i = 0; i < items.length; i++) { keysAndArgs[i + 6] = items[i].getBytes(); }
return redisTemplate.execute((RedisCallback<List<Boolean>>) connection -> connection.scriptingCommands().eval( ("return redis.call('BF.INSERT', KEYS[1], unpack(ARGV))").getBytes(), ReturnType.MULTI, 1, keysAndArgs ) ); }
public List<Boolean> mexists(String key, String... items) { if (items == null || items.length == 0) { return Collections.emptyList(); }
byte[][] keysAndArgs = new byte[1 + items.length][]; keysAndArgs[0] = key.getBytes(); for (int i = 0; i < items.length; i++) { keysAndArgs[i + 1] = items[i].getBytes(); }
return redisTemplate.execute((RedisCallback<List<Boolean>>) connection -> connection.scriptingCommands().eval( ("return redis.call('BF.MEXISTS', KEYS[1], unpack(ARGV))").getBytes(), ReturnType.MULTI, 1, keysAndArgs ) ); }
public Long card(String key) { return redisTemplate.execute((RedisCallback<Long>) connection -> connection.scriptingCommands().eval( ("return redis.call('BF.CARD', KEYS[1])").getBytes(), ReturnType.INTEGER, 1, key.getBytes() ) ); }
public Map<String, Object> info(String key) { List<Object> result = redisTemplate.execute( (RedisCallback<List<Object>>) connection -> connection.scriptingCommands().eval( "return redis.call('BF.INFO', KEYS[1])".getBytes(), ReturnType.MULTI, 1, key.getBytes() ) );
if (result == null || result.isEmpty()) { return Collections.emptyMap(); }
Map<String, Object> infoMap = new LinkedHashMap<>(); for (int i = 0; i < result.size(); i += 2) { String field = toString(result.get(i)); Object value = result.get(i + 1); infoMap.put(field, value); }
return infoMap; }
private String toString(Object obj) { if (obj instanceof byte[]) { return new String((byte[]) obj); } return String.valueOf(obj); } }
|