Created
July 24, 2014 07:53
-
-
Save sizovs/bfb34877e51aa9596c11 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fm.ask.components.counter; | |
import static org.joda.time.Duration.standardDays; | |
import org.joda.time.Duration; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import com.rubylight.conf.IConfProperty; | |
import fm.ask.dao.redis.DBCountersShardedDAOFactory; | |
import fm.ask.infrastructure.spring.config.ConfigurableLong; | |
@Configuration | |
class CounterFactoryConfiguration { | |
private final DBCountersShardedDAOFactory dbCountersShardedDAOFactory; | |
@Autowired | |
public CounterFactoryConfiguration(DBCountersShardedDAOFactory dbCountersShardedDAOFactory) { | |
this.dbCountersShardedDAOFactory = dbCountersShardedDAOFactory; | |
} | |
@Answers | |
@Bean | |
CounterFactory answers(@ConfigurableLong("counter_expire_threshold.answers_count") IConfProperty<Long> threshold) { | |
CounterExpirationPolicy expirationPolicy = rangeBased(threshold, standardDays(24)); | |
return new ShardedCounterFactory(dbCountersShardedDAOFactory, expirationPolicy) { | |
@Override | |
protected RedisCounterDAO.CounterKey counterKey(long userId) { | |
return new AnswerCounterKey(userId); | |
} | |
}; | |
} | |
@Likes | |
@Bean | |
CounterFactory likes(@ConfigurableLong("counter_expire_threshold.likes_count") IConfProperty<Long> threshold) { | |
CounterExpirationPolicy expirationPolicy = rangeBased(threshold, standardDays(24)); | |
return new ShardedCounterFactory(dbCountersShardedDAOFactory, expirationPolicy) { | |
@Override | |
protected RedisCounterDAO.CounterKey counterKey(long userId) { | |
return new LikeCounterKey(userId); | |
} | |
}; | |
} | |
@Gifts | |
@Bean | |
CounterFactory gifts(@ConfigurableLong("counter_expire_threshold.gifts_count") IConfProperty<Long> threshold) { | |
CounterExpirationPolicy expirationPolicy = rangeBased(threshold, standardDays(24)); | |
return new ShardedCounterFactory(dbCountersShardedDAOFactory, expirationPolicy) { | |
@Override | |
protected RedisCounterDAO.CounterKey counterKey(long userId) { | |
return new GiftCounterKey(userId); | |
} | |
}; | |
} | |
private CounterExpirationPolicy rangeBased(IConfProperty<Long> threshold, Duration expiration) { | |
ThresholdCounterExpirationPolicy expirationPolicy = new ThresholdCounterExpirationPolicy(threshold, expiration); | |
if (!expirationPolicy.isEnabled()) { | |
return AlwaysExpires.withExpiration(expiration); | |
} | |
return expirationPolicy; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment