All Downloads are FREE. Search and download functionalities are using the official Maven repository.

play.cache.MDCAwareTranscoder Maven / Gradle / Ivy

There is a newer version: 2.6.3
Show newest version
package play.cache;

import net.spy.memcached.transcoders.SerializingTranscoder;
import org.slf4j.MDC;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import static org.apache.commons.lang3.StringUtils.defaultString;

class MDCAwareTranscoder extends SerializingTranscoder {
  private final MemcachedTranscoder delegate;
  private final String mdcParameterName;
  private final String mdcParameterValue;

  MDCAwareTranscoder(@Nonnull MemcachedTranscoder delegate, @Nonnull String mdcParameterName, @Nonnull String mdcParameterValue) {
    this.delegate = delegate;
    this.mdcParameterName = mdcParameterName;
    this.mdcParameterValue = mdcParameterValue;
  }

  @Nullable
  @Override
  protected Object deserialize(byte[] data) {
    String originalMdcParameterValue = MDC.get(mdcParameterName);
    MDC.put(mdcParameterName, defaultString(mdcParameterValue, "?"));

    try {
      return delegate.deserialize(data);
    }
    finally {
      if (originalMdcParameterValue == null)
        MDC.remove(mdcParameterName);
      else
        MDC.put(mdcParameterName, originalMdcParameterValue);
    }
  }

  @Nullable
  @Override
  protected byte[] serialize(Object object) {
    return delegate.serialize(object);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy