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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
| package cn.openmind.cache.impl;
import cn.openmind.conf.AerospikeConf; import cn.tongdun.kara.biz.service.dal.IKVStorage; import com.aerospike.client.*; import com.aerospike.client.cdt.*; import com.aerospike.client.policy.RecordExistsAction; import com.aerospike.client.policy.WritePolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;
import java.util.Collections; import java.util.List;
@Slf4j @Component("aspKVStorage") public class AspKVStorageImpl implements IKVStorage { @Autowired @Setter private AerospikeClient aerospikeClient; @Autowired @Setter private AerospikeConf aerospikeConf;
private static final String dataSetName = "kara";
private static final String BIN = "data";
private static final Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
@Override public boolean touch(String key, int ttl) throws Exception { Key k = new Key(aerospikeConf.getNamespace(), dataSetName, key); try { WritePolicy wp = new WritePolicy(); wp.expiration = ttl; wp.setTimeout(aerospikeClient.getWritePolicyDefault().totalTimeout); aerospikeClient.touch(wp, k); } catch (AerospikeException e) { log.warn("it happens error when set ttl {} for key {} ", ttl, key); throw new Exception(e); } finally { } return true; }
@Override public long incr(String key, long by, int ttl) throws Exception { Key k = new Key(aerospikeConf.getNamespace(), dataSetName, key);
long res;
try { WritePolicy wp = new WritePolicy(); wp.expiration = ttl; wp.setTimeout(aerospikeClient.getWritePolicyDefault().totalTimeout); Operation[] opt = new Operation[]{Operation.add(new Bin(BIN, by)), Operation.get(BIN)};
Record record = aerospikeClient.operate(wp, k, opt); if (record == null) { return 0L; } res = record.getLong(BIN); } catch (AerospikeException e) { throw new Exception(e); } finally { }
return res; }
@Override public boolean delete(String key) throws Exception { Key k = new Key(aerospikeConf.getNamespace(), dataSetName, key);
boolean res; try { res = aerospikeClient.delete(null, k); } catch (AerospikeException e) { throw new Exception(e); } finally { }
return res; }
@Override public boolean exists(String key) { if (StringUtils.isBlank(key)) { return false; } Key k = new Key(aerospikeConf.getNamespace(), dataSetName, key); return aerospikeClient.exists(null, k); }
@Override public boolean set(String key, Object value, int ttl) { if (value == null) { return false; } Key k = new Key(aerospikeConf.getNamespace(), dataSetName, key); Bin bin = new Bin(BIN, value); WritePolicy wp = new WritePolicy(); wp.expiration = ttl; wp.maxRetries = 1; wp.recordExistsAction = RecordExistsAction.REPLACE; aerospikeClient.put(wp, k, bin); return true; }
public String get(String key) { if (key == null) { return null; }
try { Key k = new Key(aerospikeConf.getNamespace(), dataSetName, key); Record r = aerospikeClient.get(null, k); return r != null ? r.getString(BIN) : null; } catch (Throwable ex) { throw ex; } }
public <T> boolean lpush(String key, T... value) { if (value == null || value.length == 0) { return false; } WritePolicy policy = new WritePolicy(); Key k = new Key(aerospikeConf.getNamespace(), dataSetName, key); ListPolicy listPolicy = new ListPolicy(ListOrder.UNORDERED, ListWriteFlags.ADD_UNIQUE); for (T v : value) { if (v == null) { continue; } Value val = Value.get(v); Operation opt = ListOperation.append(listPolicy, BIN, val); try { Record rec = aerospikeClient.operate(policy, k, opt); log.info("asp lpush return record: [{}]", rec); } catch (AerospikeException e) { if (e.getResultCode() == ResultCode.ELEMENT_EXISTS) { log.warn("element [{}] already exists in [{}] key", v, key); continue; } else { return false; } } finally { } } return true; }
@Override public <T> boolean lrem(String key, T value) { Key k = new Key(aerospikeConf.getNamespace(), dataSetName, key); Operation opt = ListOperation.removeByValue(BIN, Value.get(value), ListReturnType.COUNT); Record r = aerospikeClient.operate(null, k, opt); int count = r != null ? r.getInt(BIN) : 0; log.info("asp lrem from key:[{}] remove value:[{}] count:[{}]", key, Value.getAsBlob(value), count); return true; }
@Override public List<?> lrange(String key, int start, int end) { Key k = new Key(aerospikeConf.getNamespace(), dataSetName, key); Operation opt = ListOperation.getRange(BIN, start);
Record r = aerospikeClient.operate(null, k, opt); return r != null ? r.getList(BIN) : Collections.emptyList(); } }
|