| 1 |
package org.apache.commons.jcs3.access; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
import java.io.IOException; |
| 23 |
import java.util.Set; |
| 24 |
import java.util.stream.Collectors; |
| 25 |
|
| 26 |
import org.apache.commons.jcs3.access.behavior.IGroupCacheAccess; |
| 27 |
import org.apache.commons.jcs3.access.exception.CacheException; |
| 28 |
import org.apache.commons.jcs3.access.exception.InvalidArgumentException; |
| 29 |
import org.apache.commons.jcs3.engine.CacheElement; |
| 30 |
import org.apache.commons.jcs3.engine.behavior.ICacheElement; |
| 31 |
import org.apache.commons.jcs3.engine.behavior.IElementAttributes; |
| 32 |
import org.apache.commons.jcs3.engine.control.CompositeCache; |
| 33 |
import org.apache.commons.jcs3.engine.control.group.GroupAttrName; |
| 34 |
import org.apache.commons.jcs3.engine.control.group.GroupId; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class GroupCacheAccess<K, V> |
| 40 |
extends AbstractCacheAccess<GroupAttrName<K>, V> |
| 41 |
implements IGroupCacheAccess<K, V> |
| 42 |
{ |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public GroupCacheAccess( final CompositeCache<GroupAttrName<K>, V> cacheControl ) |
| 49 |
{ |
| 50 |
super(cacheControl); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
@Override |
| 63 |
public V getFromGroup( final K name, final String group ) |
| 64 |
{ |
| 65 |
final ICacheElement<GroupAttrName<K>, V> element = this.getCacheControl().get( getGroupAttrName( group, name ) ); |
| 66 |
return ( element != null ) ? element.getVal() : null; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
private GroupAttrName<K> getGroupAttrName( final String group, final K name ) |
| 77 |
{ |
| 78 |
final GroupId gid = new GroupId( this.getCacheControl().getCacheName(), group ); |
| 79 |
return new GroupAttrName<>( gid, name ); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
@Override |
| 96 |
public void putInGroup( final K name, final String groupName, final V value ) |
| 97 |
throws CacheException |
| 98 |
{ |
| 99 |
putInGroup( name, groupName, value, null ); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
@Override |
| 118 |
public void putInGroup( final K name, final String groupName, final V value, final IElementAttributes attr ) |
| 119 |
throws CacheException |
| 120 |
{ |
| 121 |
if ( name == null ) |
| 122 |
{ |
| 123 |
throw new InvalidArgumentException( "Key must not be null" ); |
| 124 |
} |
| 125 |
|
| 126 |
if ( value == null ) |
| 127 |
{ |
| 128 |
throw new InvalidArgumentException( "Value must not be null" ); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
try |
| 134 |
{ |
| 135 |
final GroupAttrName<K> key = getGroupAttrName( groupName, name ); |
| 136 |
final CacheElement<GroupAttrName<K>, V> ce = |
| 137 |
new CacheElement<>( this.getCacheControl().getCacheName(), key, value ); |
| 138 |
|
| 139 |
final IElementAttributes attributes = (attr == null) ? this.getCacheControl().getElementAttributes() : attr; |
| 140 |
ce.setElementAttributes( attributes ); |
| 141 |
|
| 142 |
this.getCacheControl().update( ce ); |
| 143 |
} |
| 144 |
catch ( final IOException e ) |
| 145 |
{ |
| 146 |
throw new CacheException( e ); |
| 147 |
} |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
@Override |
| 158 |
public void removeFromGroup( final K name, final String group ) |
| 159 |
{ |
| 160 |
final GroupAttrName<K> key = getGroupAttrName( group, name ); |
| 161 |
this.getCacheControl().remove( key ); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
@Override |
| 171 |
public Set<K> getGroupKeys( final String group ) |
| 172 |
{ |
| 173 |
final GroupId groupId = new GroupId( this.getCacheControl().getCacheName(), group ); |
| 174 |
|
| 175 |
return this.getCacheControl().getKeySet() |
| 176 |
.stream() |
| 177 |
.filter(gan -> gan.groupId.equals(groupId)) |
| 178 |
.map(gan -> gan.attrName) |
| 179 |
.collect(Collectors.toSet()); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public Set<String> getGroupNames() |
| 188 |
{ |
| 189 |
return this.getCacheControl().getKeySet() |
| 190 |
.stream() |
| 191 |
.map(gan -> gan.groupId.groupName) |
| 192 |
.collect(Collectors.toSet()); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
@Override |
| 202 |
public void invalidateGroup( final String group ) |
| 203 |
{ |
| 204 |
this.getCacheControl().remove(getGroupAttrName(group, null)); |
| 205 |
} |
| 206 |
} |