Java Code Examples for net.spy.memcached.CachedData#getData()

The following examples show how to use net.spy.memcached.CachedData#getData() . 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: 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 3
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 4
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 5
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 6
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 7
Source File: MemcacheClientWrapper.java    From simple-spring-memcached with MIT License 4 votes vote down vote up
@Override
public CachedObject encode(final Object o) {
    CachedData cachedData = transcoder.encode(o);
    return new CachedObjectImpl(cachedData.getFlags(), cachedData.getData());
}
 
Example 8
Source File: MemcacheClientWrapper.java    From simple-spring-memcached with MIT License 4 votes vote down vote up
@Override
public CachedObject encode(final Object o) {
    CachedData cachedData = transcoder.encode(o);
    return new CachedObjectImpl(cachedData.getFlags(), cachedData.getData());
}
 
Example 9
Source File: MemcachedCache.java    From lsmtree with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] decode(final CachedData cachedData) {
    return cachedData.getData();
}
 
Example 10
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 11
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);
}