-
Notifications
You must be signed in to change notification settings - Fork 3
Aggregate Field Injection
Wiehann Matthysen edited this page Apr 15, 2012
·
2 revisions
Strawberry supports aggregate field injection. This involves injection of values from a Redis database into fields of the following type:
- List
- Map
- Set
- Ordered Set
Aggregate injection occurs when one or more keys are returned from Redis, to be injected into object instance fields of the above-mentioned types. The key pattern-matching features of Redis can be utilised to retrieve multiple key-value pairs.
To illustrate, we are going to populate our Redis database with a couple of values:
redis 127.0.0.1:6379> set test:string:01 "value 03"
OK
redis 127.0.0.1:6379> set test:string:02 "value 02"
OK
redis 127.0.0.1:6379> set test:string:03 "value 01"
OK
redis 127.0.0.1:6379> exit
Now, assuming we have a class called ConfigStore to serve as container for all of these values:
import com.github.strawberry.guice.Redis;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class ConfigStore {
@Redis("test:string:*")
private List<String> testList;
@Redis("test:string:*")
private Map<String, String> testMap;
@Redis("test:string:*")
private HashSet<String> testSet;
@Redis("test:string:*")
private LinkedHashSet<String> testOrderedSet;
public List<String> getList() {
return this.testList;
}
public Map<String, String> getMap() {
return this.testMap;
}
public Set<String> getSet() {
return this.testSet;
}
public Set<String> getOrderedSet() {
return this.testOrderedSet;
}
}Then, the following unit-test should pass:
import com.github.strawberry.guice.RedisModule;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.inject.Guice;
import com.google.inject.Injector;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
import redis.clients.jedis.JedisPool;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
public class TestConfigStore {
private final JedisPool pool = new JedisPool("localhost", 6379);
@Test
public void testConfigStoreValues() {
Injector injector = Guice.createInjector(new RedisModule(this.pool));
ConfigStore store = injector.getInstance(ConfigStore.class);
List<String> expectedList = Lists.newArrayList("value 03", "value 02", "value 01");
assertThat(store.getList(), is(equalTo(expectedList)));
Map<String, String> expectedMap = ImmutableMap.of(
"test:string:01", "value 03",
"test:string:02", "value 02",
"test:string:03", "value 01"
);
assertThat(store.getMap(), is(equalTo(expectedMap)));
Set<String> expectedSet = Sets.newHashSet("value 01", "value 02", "value 03");
assertThat(store.getSet(), is(equalTo(expectedSet)));
Set<String> expectedOrderedSet = Sets.newLinkedHashSet(
Lists.newArrayList("value 03", "value 02", "value 01")
);
assertThat(store.getOrderedSet(), is(equalTo(expectedOrderedSet)));
}
}