com.google.code.ssm.test.dao.AppUserDAOImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of integration-test Show documentation
Show all versions of integration-test Show documentation
Sample project using simple-spring-memcached
/*
* Copyright (c) 2010-2019 Jakub Białek
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.google.code.ssm.test.dao;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.stereotype.Service;
import com.google.code.ssm.api.InvalidateSingleCache;
import com.google.code.ssm.api.ParameterDataUpdateContent;
import com.google.code.ssm.api.ParameterValueKeyProvider;
import com.google.code.ssm.api.ReadThroughMultiCache;
import com.google.code.ssm.api.ReadThroughMultiCacheOption;
import com.google.code.ssm.api.ReadThroughSingleCache;
import com.google.code.ssm.api.ReturnDataUpdateContent;
import com.google.code.ssm.api.UpdateSingleCache;
import com.google.code.ssm.test.entity.AppUser;
import com.google.code.ssm.test.entity.AppUserPK;
/**
*
* @author Jakub Białek
*
*/
@Service
public class AppUserDAOImpl implements AppUserDAO {
private static final String PREFIX = "1/";
private static final String SINGLE_NS = PREFIX + "user/s";
private static final String LIST_NS = PREFIX + "user/l";
private static final Map MAP = new ConcurrentHashMap();
private static final Map> USER_ID_MAP = new ConcurrentHashMap>();
public AppUserDAOImpl() {
}
@Override
@UpdateSingleCache(namespace = SINGLE_NS, expiration = 2)
public AppUserPK create(@ParameterDataUpdateContent @ParameterValueKeyProvider AppUser entity) {
MAP.put(entity.getPK(), entity);
List list = USER_ID_MAP.get(entity.getUserId());
if (list == null) {
list = new ArrayList();
USER_ID_MAP.put(entity.getUserId(), list);
}
list.add(entity);
return entity.getPK();
}
@Override
@ReadThroughSingleCache(namespace = SINGLE_NS, expiration = 0)
public AppUser getByPk(@ParameterValueKeyProvider AppUserPK pk) {
return getByPKFromDB(pk);
}
@Override
@UpdateSingleCache(namespace = SINGLE_NS, expiration = 2)
@ReturnDataUpdateContent
public AppUser update(@ParameterValueKeyProvider AppUser entity) {
AppUser appUser = entity;
MAP.put(entity.getPK(), entity);
List list = USER_ID_MAP.get(entity.getUserId());
if (list == null) {
list = new ArrayList();
USER_ID_MAP.put(entity.getUserId(), list);
}
list.add(entity);
assert appUser.getApplicationId() == entity.getApplicationId();
assert appUser.getUserId() == entity.getUserId();
return appUser;
}
@Override
@InvalidateSingleCache(namespace = SINGLE_NS)
public void remove(@ParameterValueKeyProvider AppUserPK pk) {
AppUser appUser = MAP.remove(pk);
List list = USER_ID_MAP.get(pk.getUserId());
if (list != null) {
list.remove(appUser);
}
}
@Override
@ReadThroughMultiCache(namespace = SINGLE_NS, expiration = 0, option = @ReadThroughMultiCacheOption(generateKeysFromResult = true))
public List getList(@ParameterValueKeyProvider(order = 0) int userId,
@ParameterValueKeyProvider(order = 1) final List appsIds) {
List list = new ArrayList();
for (Integer appId : appsIds) {
AppUser au = MAP.get(new AppUserPK(userId, appId));
if (au != null) {
list.add(au);
}
}
return list;
}
@Override
@ReadThroughSingleCache(namespace = LIST_NS, expiration = 0)
public List getAppIdList(@ParameterValueKeyProvider(order = 0) int userId,
@ParameterValueKeyProvider(order = 1) boolean authorized) {
List list = USER_ID_MAP.get(userId);
List result = new ArrayList();
if (list != null) {
for (AppUser appUser : list) {
if (appUser.isEnabled() == authorized) {
result.add(appUser.getApplicationId());
}
}
}
return result;
}
@Override
@ReadThroughMultiCache(namespace = SINGLE_NS, expiration = 0, option = @ReadThroughMultiCacheOption(generateKeysFromResult = true))
public List getUsersList(@ParameterValueKeyProvider(order = 1) int applicationId,
@ParameterValueKeyProvider(order = 0) List userIds) {
List list = new ArrayList();
for (Integer userId : userIds) {
AppUser au = MAP.get(new AppUserPK(userId, applicationId));
if (au != null && au.isEnabled()) {
list.add(au);
}
}
return list;
}
@Override
@ReadThroughMultiCache(namespace = SINGLE_NS, expiration = 0, option = @ReadThroughMultiCacheOption(generateKeysFromResult = true))
public List getUsersListFromCache(@ParameterValueKeyProvider(order = 1) int applicationId,
@ParameterValueKeyProvider(order = 0) List usersIds, Collection notFoundUsersIds) {
notFoundUsersIds.addAll(usersIds);
return null;
}
@Override
@UpdateSingleCache(namespace = LIST_NS, expiration = 2)
public void updateListInCache(@ParameterValueKeyProvider(order = 0) int userId,
@ParameterValueKeyProvider(order = 1) boolean authorized, @ParameterDataUpdateContent List appsIdsList) {
// noting to do
}
@Override
@UpdateSingleCache(namespace = SINGLE_NS, expiration = 0)
@ReturnDataUpdateContent
public AppUser getByPKFromDB(@ParameterValueKeyProvider AppUserPK pk) {
AppUser appUser = MAP.get(pk);
assert appUser == null || appUser.getApplicationId() == pk.getApplicationId();
assert appUser == null || appUser.getUserId() == pk.getUserId();
return appUser;
}
}