Demo entry 5448271

test

   

Submitted by anonymous on Jun 25, 2016 at 18:08
Language: Java. Code size: 2.1 kB.

package com.toi.ccs.caches.redis;

import java.lang.reflect.Type;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.cache.Cache;
import javax.cache.configuration.FactoryBuilder;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.expiry.Duration;

import org.colombia.jcache.RedisCacheManager;
import org.colombia.jcache.codec.FormattedStringCodec;
import org.colombia.jcache.configuration.RedisCacheConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.colombia.core.Item;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lambdaworks.redis.ReadFrom;
import com.toi.ccs.CacheBuilder;
import com.toi.ccs.Caches;
import com.toi.ccs.caches.keys.PriorityClientGeographyAudienceKey;
import com.toi.util.CTNResourceBundle;
import com.toi.util.PropertyFile;

@Component
public class ItemGeoAudOrganicCacheBuilder implements CacheBuilder<PriorityClientGeographyAudienceKey, List<Item>> {
	@Autowired
	RedisCacheManager cacheRedisManager;

	Type LIST_OF_ITEMS = new TypeToken<List<Item>>() {
	}.getType();

	@Override
	public Cache<PriorityClientGeographyAudienceKey, List<Item>> buildCache() {
		Gson gson = new Gson();
		RedisCacheConfiguration<PriorityClientGeographyAudienceKey, List<Item>> redisCacheCfg = new RedisCacheConfiguration<>(
				CTNResourceBundle.getString(PropertyFile.colombia, "cache.redis.uri"),
				RedisCacheConfiguration.RedisMode.MASTER_SLAVE, ReadFrom.MASTER,
				new FormattedStringCodec<>(PriorityClientGeographyAudienceKey::fromString,
						PriorityClientGeographyAudienceKey::toString),
				new FormattedStringCodec<>(s -> gson.fromJson(s, LIST_OF_ITEMS), gson::toJson));
		redisCacheCfg.setExpiryPolicyFactory(
				FactoryBuilder.factoryOf(new CreatedExpiryPolicy(new Duration(TimeUnit.DAYS, 7))));
		return cacheRedisManager.createCache(getName(), redisCacheCfg);
	}

	@Override
	public String getName() {

		return Caches.RedisCache.ITEM_GEO_AUD_ORGANIC.cacheName();
	}

}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).