net.spy.memcached.CachedData Java Examples

The following examples show how to use net.spy.memcached.CachedData. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EVCacheRESTService.java    From EVCache with Apache License 2.0 6 votes vote down vote up
@GET
@Path("{appId}/{key}")
@Produces({MediaType.APPLICATION_OCTET_STREAM})
public Response getOperation(@PathParam("appId") String appId,
                             @PathParam("key") String key) {
    appId = appId.toUpperCase();
    if (logger.isDebugEnabled()) logger.debug("Get for application " + appId + " for Key " + key);
    try {
        final EVCache evCache = getEVCache(appId);
        CachedData cachedData = (CachedData) evCache.get(key, evcacheTranscoder);
        if (cachedData == null) {
            return Response.status(404).type("text/plain").entity("Key " + key + " Not Found in cache " + appId + "\n").build();
        }
        byte[] bytes = cachedData.getData();
        if (bytes == null) {
            return Response.status(404).type("text/plain").entity("Key " + key + " Not Found in cache " + appId + "\n").build();
        } else {
            return Response.status(200).type("application/octet-stream").entity(bytes).build();
        }
    } catch (EVCacheException e) {
        e.printStackTrace();
        return Response.serverError().build();

    }
}
 
Example #2
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 6 votes vote down vote up
public <T> Future<Boolean> appendOrAdd(String key, CachedData value, int timeToLive, EVCacheLatch evcacheLatch) throws Exception {
    final MemcachedNode node = evcacheMemcachedClient.getEVCacheNode(key);
    if (!ensureWriteQueueSize(node, key, Call.APPEND_OR_ADD)) {
        if (log.isInfoEnabled()) log.info("Node : " + node + " is not active. Failing fast and dropping the write event.");
        final ListenableFuture<Boolean, OperationCompletionListener> defaultFuture = (ListenableFuture<Boolean, OperationCompletionListener>) getDefaultFuture();
        if (evcacheLatch != null && evcacheLatch instanceof EVCacheLatchImpl && !isInWriteOnly()) ((EVCacheLatchImpl) evcacheLatch).addFuture(defaultFuture);
        return defaultFuture;
    }

    try {
        return evcacheMemcachedClient.asyncAppendOrAdd(key, timeToLive, value, evcacheLatch);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw e;
    }
}
 
Example #3
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 6 votes vote down vote up
public <T> Single<T> get(String key, Transcoder<T> tc, boolean _throwException, boolean hasZF, boolean chunked, Scheduler scheduler)  throws Exception {
    if (chunked) {
        return assembleChunks(key, _throwException, 0, tc, hasZF, scheduler);
    }  else if(shouldHashKey()) {
        final String hKey = getHashedKey(key);
        final Object obj = evcacheMemcachedClient.asyncGet(hKey, evcacheValueTranscoder, null).get(readTimeout.get(), TimeUnit.MILLISECONDS, _throwException, hasZF);
        if(obj instanceof EVCacheValue) {
            final EVCacheValue val = (EVCacheValue)obj;
            if(val == null || !(val.getKey().equals(key))) {
                incrementFailure(EVCacheMetricsFactory.KEY_HASH_COLLISION, Call.GET);
                return null;
            }
            final CachedData cd = new CachedData(val.getFlags(), val.getValue(), CachedData.MAX_SIZE);
            if(tc == null) {
                return Single.just((T)evcacheMemcachedClient.getTranscoder().decode(cd));
            } else {
                return Single.just(tc.decode(cd));
            }
        } else {
            return null;
        }
    } else {
        return evcacheMemcachedClient.asyncGet(key, tc, null)
            .get(readTimeout.get(), TimeUnit.MILLISECONDS, _throwException, hasZF, scheduler);
    }
}
 
Example #4
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 6 votes vote down vote up
public <T> T get(String key, Transcoder<T> tc, boolean _throwException, boolean hasZF, boolean chunked) throws Exception {
    if (chunked) {
        return assembleChunks(key, false, 0, tc, hasZF);
    } else if(shouldHashKey()) {
        final String hKey = getHashedKey(key);
        final Object obj = evcacheMemcachedClient.asyncGet(hKey, evcacheValueTranscoder, null).get(readTimeout.get(), TimeUnit.MILLISECONDS, _throwException, hasZF);
        if(obj instanceof EVCacheValue) {
            final EVCacheValue val = (EVCacheValue)obj;
            if(val == null || !(val.getKey().equals(key))) {
                incrementFailure(EVCacheMetricsFactory.KEY_HASH_COLLISION, Call.GET);
                return null;
            }
            final CachedData cd = new CachedData(val.getFlags(), val.getValue(), CachedData.MAX_SIZE);
            if(tc == null) {
                return (T)evcacheMemcachedClient.getTranscoder().decode(cd);
            } else {
                return tc.decode(cd);
            }
        } else {
            return null;
        }
    } else {
        return evcacheMemcachedClient.asyncGet(key, tc, null).get(readTimeout.get(),
                TimeUnit.MILLISECONDS, _throwException, hasZF);
    }
}
 
Example #5
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves all the chunks as is. This is mainly used for debugging.
 *
 * @param key
 * @return Returns all the chunks retrieved.
 * @throws EVCacheReadQueueException
 * @throws EVCacheException
 * @throws Exception
 */
public Map<String, CachedData> getAllChunks(String key) throws EVCacheReadQueueException, EVCacheException, Exception {
    try {
        final ChunkDetails<Object> cd = getChunkDetails(key);
        if(log.isDebugEnabled()) log.debug("Chunkdetails " + cd);
        if (cd == null) return null;
        if (!cd.isChunked()) {
            Map<String, CachedData> rv = new HashMap<String, CachedData>();
            rv.put(key, (CachedData) cd.getData());
            if(log.isDebugEnabled()) log.debug("Data : " + rv);
            return rv;
        } else {
            final List<String> keys = cd.getChunkKeys();
            if(log.isDebugEnabled()) log.debug("Keys - " + keys);
            final Map<String, CachedData> dataMap = evcacheMemcachedClient.asyncGetBulk(keys, chunkingTranscoder, null)
                    .getSome(readTimeout.get().intValue(), TimeUnit.MILLISECONDS, false, false);

            if(log.isDebugEnabled()) log.debug("Datamap " + dataMap);
            return dataMap;
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    return null;
}
 
Example #6
Source File: EVCacheClientUtil.java    From EVCache with Apache License 2.0 6 votes vote down vote up
private EVCacheLatch fixup(EVCacheClient sourceClient, EVCacheClient[] destClients, EVCacheKey evcKey, int timeToLive, Policy policy) {
    final EVCacheLatchImpl latch = new EVCacheLatchImpl(policy, destClients.length, _appName);
    try {
        final CachedData readData = sourceClient.get(evcKey.getDerivedKey(sourceClient.isDuetClient()), ct, false, false);

        if(readData != null) {
            sourceClient.touch(evcKey.getDerivedKey(sourceClient.isDuetClient()), timeToLive);
            for(EVCacheClient destClient : destClients) {
                destClient.set(evcKey.getDerivedKey(destClient.isDuetClient()), readData, timeToLive, latch);
            }
        }
        latch.await(_pool.getOperationTimeout().get(), TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        log.error("Error reading the data", e);
    }
    return latch;
}
 
Example #7
Source File: KryoTranscoder.java    From hibernate4-memcached with Apache License 2.0 6 votes vote down vote up
@Override
public CachedData encode(Object object) {
    int flags = BASE_FLAG;

    byte[] encodedBytes = kryoEncode(object);

    boolean compressionRequired = encodedBytes.length > compressionThreasholdBytes;

    if (compressionRequired) {
        int beforeSize = encodedBytes.length;
        encodedBytes = compress(encodedBytes);

        log.debug("kryotranscoder compress required : {}, original {} bytes -> compressed {} bytes", compressionRequired, beforeSize, encodedBytes.length);
        flags = flags | COMPRESS_FLAG;
    }
    return new CachedData(flags, encodedBytes, getMaxSize());
}
 
Example #8
Source File: KryoTranscoder.java    From springboot-shiro-cas-mybatis with MIT License 5 votes vote down vote up
@Override
public CachedData encode(final Object obj) {
    final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    try (final Output output = new Output(byteStream)) {
        kryo.writeClassAndObject(output, obj);
        output.flush();
        final byte[] bytes = byteStream.toByteArray();
        return new CachedData(0, bytes, bytes.length);
    }
}
 
Example #9
Source File: MemcachedBlockCache.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public HFileBlock decode(CachedData d) {
  try {
    ByteBuff buf = new SingleByteBuff(ByteBuffer.wrap(d.getData()));
    return (HFileBlock) HFileBlock.BLOCK_DESERIALIZER.deserialize(buf, ByteBuffAllocator.HEAP);
  } catch (IOException e) {
    LOG.warn("Failed to deserialize data from memcached", e);
  }
  return null;
}
 
Example #10
Source File: KryoTranscoder.java    From cas4.0.x-server-wechat with Apache License 2.0 5 votes vote down vote up
@Override
public Object decode(final CachedData d) {
    final byte[] bytes = d.getData();
    final Input input = new Input(new ByteArrayInputStream(bytes));
    final Object obj =  kryo.readClassAndObject(input);
    IOUtils.closeQuietly(input);
    return obj;
}
 
Example #11
Source File: KryoTranscoder.java    From cas4.0.x-server-wechat with Apache License 2.0 5 votes vote down vote up
@Override
public CachedData encode(final Object obj) {
    final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    final Output output = new Output(byteStream);
    kryo.writeClassAndObject(output, obj);
    output.flush();
    IOUtils.closeQuietly(output);
    final byte[] bytes = byteStream.toByteArray();
    return new CachedData(0, bytes, bytes.length);
}
 
Example #12
Source File: KryoTranscoderTests.java    From springboot-shiro-cas-mybatis with MIT License 5 votes vote down vote up
@Test
public void verifyEncodeDecodeTGTImpl() throws Exception {
    final Credential userPassCredential = new UsernamePasswordCredential(USERNAME, PASSWORD);
    final AuthenticationBuilder bldr = new DefaultAuthenticationBuilder(
            new DefaultPrincipalFactory()
                    .createPrincipal("user", Collections.unmodifiableMap(this.principalAttributes)));
    bldr.setAttributes(Collections.unmodifiableMap(this.principalAttributes));
    bldr.setAuthenticationDate(new Date());
    bldr.addCredential(new BasicCredentialMetaData(userPassCredential));
    bldr.addFailure("error", AccountNotFoundException.class);
    bldr.addSuccess("authn", new DefaultHandlerResult(
            new AcceptUsersAuthenticationHandler(),
            new BasicCredentialMetaData(userPassCredential)));

    final TicketGrantingTicket parent =
            new TicketGrantingTicketImpl(TGT_ID, TestUtils.getService(), null, bldr.build(),
                    new NeverExpiresExpirationPolicy());

    final TicketGrantingTicket expectedTGT =
            new TicketGrantingTicketImpl(TGT_ID, TestUtils.getService(),
                    null, bldr.build(),
                    new NeverExpiresExpirationPolicy());

    final ServiceTicket ticket = expectedTGT.grantServiceTicket(ST_ID,
            TestUtils.getService(),
            new NeverExpiresExpirationPolicy(), false);
    CachedData result = transcoder.encode(expectedTGT);
    final TicketGrantingTicket resultTicket = (TicketGrantingTicket) transcoder.decode(result);

    assertEquals(expectedTGT, resultTicket);
    result = transcoder.encode(ticket);
    final ServiceTicket resultStTicket = (ServiceTicket) transcoder.decode(result);
    assertEquals(ticket, resultStTicket);

}
 
Example #13
Source File: KryoTranscoder.java    From springboot-shiro-cas-mybatis with MIT License 5 votes vote down vote up
@Override
public Object decode(final CachedData d) {
    final byte[] bytes = d.getData();
    try (final Input input = new Input(new ByteArrayInputStream(bytes))) {
        final Object obj =  kryo.readClassAndObject(input);
        return obj;
    }
}
 
Example #14
Source File: KryoTranscoderTest.java    From hibernate4-memcached with Apache License 2.0 5 votes vote down vote up
@Test
public void kryoEncodeDecode_with_compression() throws Exception {
    givenKryoTranscoder(LOREM_IPSUM_BYTES.length / 2);

    CachedData cachedData = kryoTranscoder.encode(LOREM_IPSUM);
    assertThat(cachedData.getFlags() & KryoTranscoder.COMPRESS_FLAG).isGreaterThanOrEqualTo(1);

    log.debug("compressed data size : {} / original : {}", cachedData.getData().length, LOREM_IPSUM_BYTES.length);
    assertThat(cachedData.getData().length).isLessThan(LOREM_IPSUM_BYTES.length);

    Object decoded = kryoTranscoder.decode(cachedData);
    assertThat(decoded).isEqualTo(LOREM_IPSUM);
}
 
Example #15
Source File: KryoTranscoder.java    From hibernate4-memcached with Apache License 2.0 5 votes vote down vote up
@Override
public Object decode(CachedData data) {
    int flags = data.getFlags();

    byte[] decodedBytes = data.getData();

    boolean compressed = (flags & COMPRESS_FLAG) > 0;

    if (compressed) {
        decodedBytes = decompress(decodedBytes);
    }

    Kryo kryo = createKryo();
    return kryo.readClassAndObject(new Input(decodedBytes));
}
 
Example #16
Source File: KryoTranscoderTest.java    From hibernate4-memcached with Apache License 2.0 5 votes vote down vote up
@Test
public void kryoEncodeDecode() throws Exception {
    givenKryoTranscoder(LOREM_IPSUM_BYTES.length * 2);
    CachedData cachedData = kryoTranscoder.encode(LOREM_IPSUM);

    assertThat(cachedData.getFlags()).isEqualTo(KryoTranscoder.BASE_FLAG);

    log.debug("normal data size : {} / original {}", cachedData.getData().length, LOREM_IPSUM_BYTES.length);
    assertThat(cachedData.getData().length).isGreaterThan(LOREM_IPSUM_BYTES.length); //not compressed

    Object decoded = kryoTranscoder.decode(cachedData);
    assertThat(decoded).isEqualTo(LOREM_IPSUM);
}
 
Example #17
Source File: EVCacheTracingEventListener.java    From EVCache with Apache License 2.0 5 votes vote down vote up
private void onFinishHelper(EVCacheEvent e, Throwable t) {
  Object clientSpanObj = e.getAttribute(CLIENT_SPAN_ATTRIBUTE_KEY);

  // Return if the previously saved Client Span is null
  if (clientSpanObj == null) {
    return;
  }

  Span clientSpan = (Span) clientSpanObj;

  try {
    if (t != null) {
      this.safeTag(clientSpan, EVCacheTracingTags.ERROR, t.toString());
    }

    String status = e.getStatus();
    this.safeTag(clientSpan, EVCacheTracingTags.STATUS, status);

    long latency = this.getDurationInMicroseconds(e.getDurationInMillis());
    clientSpan.tag(EVCacheTracingTags.LATENCY, String.valueOf(latency));

    int ttl = e.getTTL();
    clientSpan.tag(EVCacheTracingTags.DATA_TTL, String.valueOf(ttl));

    CachedData cachedData = e.getCachedData();
    if (cachedData != null) {
      int cachedDataSize = cachedData.getData().length;
      clientSpan.tag(EVCacheTracingTags.DATA_SIZE, String.valueOf(cachedDataSize));
    }
  } finally {
    clientSpan.finish();
  }
}
 
Example #18
Source File: EVCacheClientUtil.java    From EVCache with Apache License 2.0 5 votes vote down vote up
/**
 * TODO : once metaget is available we need to get the remaining ttl from an existing entry and use it 
 */
public EVCacheLatch add(EVCacheKey evcKey, CachedData cd, boolean shouldHashKey, Transcoder evcacheValueTranscoder, int timeToLive, Policy policy) throws Exception {
    if (cd == null) return null; 
    
    final EVCacheClient[] clients = _pool.getEVCacheClientForWrite();
    final EVCacheLatchImpl latch = new EVCacheLatchImpl(policy, clients.length - _pool.getWriteOnlyEVCacheClients().length, _appName);

    Boolean firstStatus = null;
    for (EVCacheClient client : clients) {
        String key = evcKey.getDerivedKey(client.isDuetClient());
        if (shouldHashKey) {
            final EVCacheValue val = new EVCacheValue(evcKey.getCanonicalKey(client.isDuetClient()), cd.getData(), cd.getFlags(), timeToLive, System.currentTimeMillis());
            cd = evcacheValueTranscoder.encode(val);
        }
        final Future<Boolean> f = client.add(key, timeToLive, cd, latch);
        if (log.isDebugEnabled()) log.debug("ADD : Op Submitted : APP " + _appName + ", key " + key + "; future : " + f + "; client : " + client);
        boolean status = f.get().booleanValue();
        if(!status) { // most common case
            if(firstStatus == null) {
                for(int i = 0; i < clients.length; i++) {
                    latch.countDown();
                }
                return latch;
            } else {
                return fixup(client, clients, evcKey, timeToLive, policy);
            }
        }
        if(firstStatus == null) firstStatus = Boolean.valueOf(status);
    }
    return latch;
}
 
Example #19
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 5 votes vote down vote up
private <T> ChunkDetails<T> getChunkDetails(String key) {

        final List<String> firstKeys = new ArrayList<String>(2);
        firstKeys.add(key);
        final String firstKey = key + "_00";
        firstKeys.add(firstKey);
        try {
            final Map<String, CachedData> metadataMap = evcacheMemcachedClient.asyncGetBulk(firstKeys, chunkingTranscoder, null)
                    .getSome(readTimeout.get(), TimeUnit.MILLISECONDS, false, false);
            if (metadataMap.containsKey(key)) {
                return new ChunkDetails(null, null, false, metadataMap.get(key));
            } else if (metadataMap.containsKey(firstKey)) {
                final ChunkInfo ci = getChunkInfo(firstKey, (String) decodingTranscoder.decode(metadataMap.get(firstKey)));
                if (ci == null) return null;

                final List<String> keys = new ArrayList<>();
                for (int i = 1; i < ci.getChunks(); i++) {
                    final String prefix = (i < 10) ? "0" : "";
                    keys.add(ci.getKey() + "_" + prefix + i);
                }
                return new ChunkDetails(keys, ci, true, null);
            } else {
                return null;
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }
 
Example #20
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 5 votes vote down vote up
private Future<Boolean> _replace(String key, CachedData value, int timeToLive, EVCacheLatch evcacheLatch) throws Exception {
    final MemcachedNode node = evcacheMemcachedClient.getEVCacheNode(key);
    if (!ensureWriteQueueSize(node, key, Call.REPLACE)) {
        if (log.isInfoEnabled()) log.info("Node : " + node + " is not active. Failing fast and dropping the replace event.");
        final ListenableFuture<Boolean, OperationCompletionListener> defaultFuture = (ListenableFuture<Boolean, OperationCompletionListener>) getDefaultFuture();
        if (evcacheLatch != null && evcacheLatch instanceof EVCacheLatchImpl && !isInWriteOnly()) ((EVCacheLatchImpl) evcacheLatch).addFuture(defaultFuture);
        return defaultFuture;
    }

    try {
        final int dataSize = ((CachedData) value).getData().length;
        if (enableChunking.get() && dataSize > chunkSize.get()) {
            final CachedData[] cd = createChunks(value, key);
            final int len = cd.length;
            final OperationFuture<Boolean>[] futures = new OperationFuture[len];
            for (int i = 0; i < cd.length; i++) {
                final String prefix = (i < 10) ? "0" : "";
                futures[i] = evcacheMemcachedClient.replace(key + "_" + prefix + i, timeToLive, cd[i], null, null);
            }
            return new EVCacheFutures(futures, key, appName, serverGroup, evcacheLatch);
        } else if(shouldHashKey()) {
            final String hKey = getHashedKey(key);
            final CachedData cVal = getEVCacheValue(key, value, timeToLive);
            return evcacheMemcachedClient.replace(hKey, timeToLive, cVal, null, evcacheLatch);
        } else {
            return evcacheMemcachedClient.replace(key, timeToLive, value, null, evcacheLatch);
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw e;
    }
}
 
Example #21
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 5 votes vote down vote up
private Future<Boolean> _add(String key, int exp, CachedData value, EVCacheLatch latch) throws Exception {
    if (enableChunking.get()) throw new EVCacheException("This operation is not supported as chunking is enabled on this EVCacheClient.");

    final MemcachedNode node = evcacheMemcachedClient.getEVCacheNode(key);
    if (!ensureWriteQueueSize(node, key, Call.ADD)) return getDefaultFuture();
    if(shouldHashKey()) {
        final String hKey = getHashedKey(key);
        final CachedData cVal = getEVCacheValue(key, value, exp);
        return evcacheMemcachedClient.add(hKey, exp, cVal, null, latch);
    } else {
        return evcacheMemcachedClient.add(key, exp, value, null, latch);
    }
}
 
Example #22
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 4 votes vote down vote up
private CachedData[] createChunks(CachedData cd, String key) {
    final int cSize = chunkSize.get();
    if ((key.length() + 3) > cSize) throw new IllegalArgumentException("The chunksize " + cSize
            + " is smaller than the key size. Will not be able to proceed. key size = "
            + key.length());
    final int len = cd.getData().length;

    /* the format of headers in memcached */
    // Key size + 1 + Header( Flags (Characters Number) + Key (Characters Numbers) + 2 bytes ( \r\n ) + 4 bytes (2 spaces and 1 \r)) + Chunk Size + CAS Size
    // final int overheadSize = key.length() // Key Size
    // + 1 // Space
    // + 4 // Flags (Characters Number)
    // + 4 // Key (Characters Numbers)
    // + 2 // /r/n
    // + 4 // 2 spaces and 1 \r
    // + 48 // Header Size
    // + 8; // CAS
    final int overheadSize = key.length() + 71 + 3;
    // 3 because we will suffix _00, _01 ... _99; 68 is the size of the memcached header
    final int actualChunkSize = cSize - overheadSize;
    int lastChunkSize = len % actualChunkSize;
    final int numOfChunks = len / actualChunkSize + ((lastChunkSize > 0) ? 1 : 0) + 1;
    final CachedData[] chunkData = new CachedData[numOfChunks];
    if (lastChunkSize == 0) lastChunkSize = actualChunkSize;

    final long sTime = System.nanoTime();
    final Checksum checksum = new CRC32();
    checksum.update(cd.getData(), 0, len);
    final long checkSumValue = checksum.getValue();

    int srcPos = 0;
    if (log.isDebugEnabled()) log.debug("Ths size of data is " + len + " ; we will create " + (numOfChunks - 1)
            + " of " + actualChunkSize + " bytes. Checksum : "
            + checkSumValue + "; Checksum Duration : " + (System.nanoTime() - sTime));
    chunkData[0] = decodingTranscoder.encode(numOfChunks + ":" + actualChunkSize + ":" + lastChunkSize + ":" + cd
            .getFlags() + ":" + checkSumValue);
    for (int i = 1; i < numOfChunks; i++) {
        int lengthOfArray = actualChunkSize;
        if (srcPos + actualChunkSize > len) {
            lengthOfArray = len - srcPos;
        }
        byte[] dest = new byte[actualChunkSize];
        System.arraycopy(cd.getData(), srcPos, dest, 0, lengthOfArray);
        if (actualChunkSize > lengthOfArray) {
            for (int j = lengthOfArray; j < actualChunkSize; j++) {
                dest[j] = Character.UNASSIGNED;// Adding filler data
            }
        }
        srcPos += lengthOfArray;
        //chunkData[i] = decodingTranscoder.encode(dest);
        chunkData[i] = new CachedData(SPECIAL_BYTEARRAY, dest, Integer.MAX_VALUE);
    }
    EVCacheMetricsFactory.getInstance().getDistributionSummary(EVCacheMetricsFactory.INTERNAL_NUM_CHUNK_SIZE, getTagList()).record(numOfChunks);
    EVCacheMetricsFactory.getInstance().getDistributionSummary(EVCacheMetricsFactory.INTERNAL_CHUNK_DATA_SIZE, getTagList()).record(len);

    return chunkData;
}
 
Example #23
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 4 votes vote down vote up
public Future<Boolean> replace(String key, CachedData cd, int timeToLive, EVCacheLatch evcacheLatch) throws Exception {
    return _replace(key, cd, timeToLive, evcacheLatch);
}
 
Example #24
Source File: TranscoderAdapter.java    From simple-spring-memcached with MIT License 4 votes vote down vote up
@Override
public Object decode(final CachedData d) {
    return transcoder.decode(new CachedObjectWrapper(d));
}
 
Example #25
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 4 votes vote down vote up
protected CachedData getEVCacheValue(String key, CachedData cData, int timeToLive) {
    final EVCacheValue val = new EVCacheValue(key, cData.getData(), cData.getFlags(), timeToLive, System.currentTimeMillis());
    return evcacheValueTranscoder.encode(val);
}
 
Example #26
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 4 votes vote down vote up
public Future<Boolean> set(String key, CachedData value, int timeToLive) throws Exception {
    return _set(key, value, timeToLive, null);
}
 
Example #27
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 4 votes vote down vote up
public <T> Map<String, T> getBulk(Collection<String> _canonicalKeys, Transcoder<T> tc, boolean _throwException,
        boolean hasZF) throws Exception {
    final Collection<String> canonicalKeys = validateReadQueueSize(_canonicalKeys, Call.BULK);
    final Map<String, T> returnVal;
    try {
        if (tc == null) tc = (Transcoder<T>) getTranscoder();
        if (enableChunking.get()) {
            returnVal = assembleChunks(_canonicalKeys, tc, hasZF);
        } else if(shouldHashKey()) {
            final Collection<String> hashKeys = new ArrayList<String>(canonicalKeys.size());
            for(String cKey : canonicalKeys) {
                final String hKey = getHashedKey(cKey);
                hashKeys.add(hKey);
            }
            final Map<String, Object> vals = evcacheMemcachedClient.asyncGetBulk(hashKeys, evcacheValueTranscoder, null).getSome(bulkReadTimeout.get(), TimeUnit.MILLISECONDS, _throwException, hasZF);
            if(vals != null && !vals.isEmpty()) {
                returnVal = new HashMap<String, T>(vals.size());
                for(Entry<String, Object> entry : vals.entrySet()) {
                    final Object obj = entry.getValue();
                    if(obj instanceof EVCacheValue) {
                        final EVCacheValue val = (EVCacheValue)obj;
                        final CachedData cd = new CachedData(val.getFlags(), val.getValue(), CachedData.MAX_SIZE);
                        if(tc == null) {
                            returnVal.put(val.getKey(), (T)evcacheMemcachedClient.getTranscoder().decode(cd));
                        } else {
                            returnVal.put(val.getKey(), tc.decode(cd));
                        }
                    } else {
                        if (log.isDebugEnabled()) log.debug("Value for key : " + entry.getKey() + " is not EVCacheValue. val : " + obj);
                    }
                }
            } else {
                return Collections.<String, T> emptyMap();
            }
        } else {
            returnVal = evcacheMemcachedClient.asyncGetBulk(canonicalKeys, tc, null)
                    .getSome(bulkReadTimeout.get(), TimeUnit.MILLISECONDS, _throwException, hasZF);
        }
    } catch (Exception e) {
        if (_throwException) throw e;
        return Collections.<String, T> emptyMap();
    }
    return returnVal;
}
 
Example #28
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 4 votes vote down vote up
public <T> Single<Map<String, T>> getBulk(Collection<String> _canonicalKeys, final Transcoder<T> transcoder, boolean _throwException,
        boolean hasZF, Scheduler scheduler) {
    try {
        final Collection<String> canonicalKeys = validateReadQueueSize(_canonicalKeys, Call.BULK);
        final Transcoder<T> tc = (transcoder == null) ? (Transcoder<T>) getTranscoder() : transcoder;
        if (enableChunking.get()) {
            return assembleChunks(_canonicalKeys, tc, hasZF, scheduler);
        } else if(shouldHashKey()) {
            final Collection<String> hashKeys = new ArrayList<String>(canonicalKeys.size());
            for(String cKey : canonicalKeys) {
                final String hKey = getHashedKey(cKey);
                hashKeys.add(hKey);
            }
            final Single<Map<String, Object>> vals = evcacheMemcachedClient.asyncGetBulk(hashKeys, evcacheValueTranscoder, null).getSome(bulkReadTimeout.get(), TimeUnit.MILLISECONDS, _throwException, hasZF, scheduler);
            if(vals != null ) {
                return vals.flatMap(r -> {
                    HashMap<String, T> returnVal = new HashMap<String, T>();
                    for(Entry<String, Object> entry : r.entrySet()) {
                        final Object obj = entry.getValue();
                        if(obj instanceof EVCacheValue) {
                            final EVCacheValue val = (EVCacheValue)obj;
                            final CachedData cd = new CachedData(val.getFlags(), val.getValue(), CachedData.MAX_SIZE);
                            if(tc == null) {
                                returnVal.put(val.getKey(), (T)evcacheMemcachedClient.getTranscoder().decode(cd));
                            } else {
                                returnVal.put(val.getKey(), tc.decode(cd));
                            }
                        } else {
                            if (log.isDebugEnabled()) log.debug("Value for key : " + entry.getKey() + " is not EVCacheValue. val : " + obj);
                        }
                    }
                    return Single.just(returnVal);
                });
            } else {
                return Single.just(Collections.<String, T> emptyMap());
            }
        } else {
            return evcacheMemcachedClient.asyncGetBulk(canonicalKeys, tc, null)
                .getSome(bulkReadTimeout.get(), TimeUnit.MILLISECONDS, _throwException, hasZF, scheduler);
        }
    } catch (Throwable e) {
        return Single.error(e);
    }
}
 
Example #29
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 4 votes vote down vote up
private Future<Boolean> _set(String key, CachedData value, int timeToLive, EVCacheLatch evcacheLatch) throws Exception {
    final MemcachedNode node = evcacheMemcachedClient.getEVCacheNode(key);
    if (!ensureWriteQueueSize(node, key, Call.SET)) {
        if (log.isInfoEnabled()) log.info("Node : " + node + " is not active. Failing fast and dropping the write event.");
        final ListenableFuture<Boolean, OperationCompletionListener> defaultFuture = (ListenableFuture<Boolean, OperationCompletionListener>) getDefaultFuture();
        if (evcacheLatch != null && evcacheLatch instanceof EVCacheLatchImpl && !isInWriteOnly()) ((EVCacheLatchImpl) evcacheLatch).addFuture(defaultFuture);
        return defaultFuture;
    }

    try {
        final int dataSize = ((CachedData) value).getData().length;

        if (enableChunking.get()) {
            if (dataSize > chunkSize.get()) {
                final CachedData[] cd = createChunks(value, key);
                final int len = cd.length;
                final OperationFuture<Boolean>[] futures = new OperationFuture[len];
                for (int i = 0; i < cd.length; i++) {
                    final String prefix = (i < 10) ? "0" : "";
                    futures[i] = evcacheMemcachedClient.set(key + "_" + prefix + i, timeToLive, cd[i], null, null);
                }
                // ensure we are deleting the unchunked key if it exists.
                // Ignore return value since it may not exist.
                evcacheMemcachedClient.delete(key);
                return new EVCacheFutures(futures, key, appName, serverGroup, evcacheLatch);
            } else {
                // delete all the chunks if they exist as the
                // data is moving from chunked to unchunked
                delete(key);
                return evcacheMemcachedClient.set(key, timeToLive, value, null, evcacheLatch);
            }
        } else if(shouldHashKey()) {
            final String hKey = getHashedKey(key);
            final CachedData cVal = getEVCacheValue(key, value, timeToLive);
            return evcacheMemcachedClient.set(hKey, timeToLive, cVal, null, evcacheLatch);
        } else {
            return evcacheMemcachedClient.set(key, timeToLive, value, null, evcacheLatch);
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw e;
    }
}
 
Example #30
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 4 votes vote down vote up
public Future<Boolean> set(String key, CachedData cd, int timeToLive, EVCacheLatch evcacheLatch) throws Exception {
    return _set(key, cd, timeToLive, evcacheLatch);
}