Skip to content

Commit

Permalink
修改redislist接口
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangwenping committed Mar 29, 2017
1 parent 434dc60 commit 655089f
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.snowcattle.game.db.cache.redis;

/**
* Created by jiangwenping on 17/3/29.
* 异步存储的时候异步key
*/
public interface AsyncCacheKey {
public String getAsyncCacheKey();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
*/
public enum RedisKeyEnum {

PLAYER("wp#"),
PLAYER("pr#"),
ASYNC_PLAYER("ay_pr"),
;

private String key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
* 列表类型的缓存对象
*
*/
public interface RedisListInterface extends RedisInterface{
public interface RedisListInterface{
public String getUniqueKey();
public String getRedisKeyEnumString();

/**
* 列表对象的子唯一主键属性
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.snowcattle.game.db.service.proxy;

import com.snowcattle.game.db.cache.redis.AsyncCacheKey;
import com.snowcattle.game.db.cache.redis.RedisInterface;
import com.snowcattle.game.db.cache.redis.RedisListInterface;
import com.snowcattle.game.db.cache.redis.RedisService;
Expand Down Expand Up @@ -43,6 +44,17 @@ public Object intercept(Object obj, Method method, Object[] args, MethodProxy me
case insert:
BaseEntity baseEntity = (BaseEntity) args[0];
updateAllFieldEntity(baseEntity);
//放入异步存储的key
if(baseEntity instanceof AsyncCacheKey) {
AsyncCacheKey asyncCacheKeyEntity = (AsyncCacheKey) baseEntity;
if(baseEntity instanceof RedisInterface) {
redisService.lpushString(asyncCacheKeyEntity.getAsyncCacheKey(), EntityUtils.getRedisKey((RedisInterface) baseEntity));
}else{
proxyLogger.error("async save interface not RedisInterface " + baseEntity.getClass().getSimpleName() + " use RedisListInterface " + baseEntity.toString());
}
}else{
proxyLogger.error("async save interface not asynccachekey " + baseEntity.getClass().getSimpleName() + " use " + baseEntity.toString());
}
break;
case insertBatch:
List<BaseEntity> entityList = (List<BaseEntity>) args[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ protected void updateChangedFieldEntity(BaseEntity entity){
RedisListInterface redisListInterface = (RedisListInterface) entity;
List<RedisListInterface> redisListInterfaceList = new ArrayList<>();
redisListInterfaceList.add(redisListInterface);
redisService.setListToHash(EntityUtils.getRedisKey(redisListInterface), redisListInterfaceList);
redisService.setListToHash(EntityUtils.getRedisKeyByRedisListInterface(redisListInterface), redisListInterfaceList);
}
}
}
Expand All @@ -149,7 +149,7 @@ protected void updateAllFieldEntity(BaseEntity entity){
RedisListInterface redisListInterface = (RedisListInterface) entity;
List<RedisListInterface> redisListInterfaceList = new ArrayList<>();
redisListInterfaceList.add(redisListInterface);
redisService.setListToHash(EntityUtils.getRedisKey(redisListInterface), redisListInterfaceList);
redisService.setListToHash(EntityUtils.getRedisKeyByRedisListInterface(redisListInterface), redisListInterfaceList);
}
}
}
Expand All @@ -165,7 +165,7 @@ protected void deleteEntity(BaseEntity baseEntity){
redisService.deleteKey(EntityUtils.getRedisKey(redisInterface));
}else if(baseEntity instanceof RedisListInterface){
RedisListInterface redisListInterface = (RedisListInterface) baseEntity;
redisService.hdel(EntityUtils.getRedisKey(redisListInterface), redisListInterface.getSubUniqueKey());
redisService.hdel(EntityUtils.getRedisKeyByRedisListInterface(redisListInterface), redisListInterface.getSubUniqueKey());
}
}
}
Expand Down Expand Up @@ -211,7 +211,7 @@ protected void updateChangedFieldEntityList(List<BaseEntity> entityList){
RedisListInterface redisListInterface = (RedisListInterface) baseEntity;
redisListInterfaceList.add(redisListInterface);
}
redisService.setListToHash(EntityUtils.getRedisKey((RedisListInterface) entity), redisListInterfaceList);
redisService.setListToHash(EntityUtils.getRedisKeyByRedisListInterface((RedisListInterface) entity), redisListInterfaceList);
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/snowcattle/game/db/util/EntityUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.snowcattle.game.db.util;

import com.snowcattle.game.db.cache.redis.RedisInterface;
import com.snowcattle.game.db.cache.redis.RedisListInterface;
import com.snowcattle.game.db.common.annotation.FieldSave;
import com.snowcattle.game.db.entity.IEntity;

Expand Down Expand Up @@ -59,4 +60,9 @@ public static Field[] getAllCacheFields(IEntity obj){
public static String getRedisKey(RedisInterface redisInterface){
return redisInterface.getRedisKeyEnumString() + redisInterface.getUniqueKey();
}

//获取rediskey
public static String getRedisKeyByRedisListInterface(RedisListInterface redisInterface){
return redisInterface.getRedisKeyEnumString() + redisInterface.getUniqueKey();
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.snowcattle.game.db.service.cache;

import com.snowcattle.game.db.cache.redis.RedisInterface;
import com.snowcattle.game.db.cache.redis.RedisListInterface;
import com.snowcattle.game.db.cache.redis.RedisService;
import com.snowcattle.game.db.entity.IEntity;
import com.snowcattle.game.db.service.jdbc.entity.MoreOrder;
import com.snowcattle.game.db.service.jdbc.entity.Order;
import com.snowcattle.game.db.service.proxy.EnityProxyService;
import com.snowcattle.game.db.util.EntityUtils;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
Expand All @@ -27,9 +24,9 @@ public static void main(String[] args) throws Exception {
moreOrder.setStatus("list");
List<RedisListInterface> list = new ArrayList<RedisListInterface>();
list.add(moreOrder);
redisService.setListToHash(EntityUtils.getRedisKey(moreOrder), list);
redisService.setListToHash(EntityUtils.getRedisKeyByRedisListInterface(moreOrder), list);

list = redisService.getListFromHash(EntityUtils.getRedisKey(moreOrder), MoreOrder.class);
list = redisService.getListFromHash(EntityUtils.getRedisKeyByRedisListInterface(moreOrder), MoreOrder.class);
System.out.println(list);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.snowcattle.game.db.service.jdbc.entity;

import com.snowcattle.game.db.cache.redis.AsyncCacheKey;
import com.snowcattle.game.db.cache.redis.RedisInterface;
import com.snowcattle.game.db.cache.redis.RedisKeyEnum;
import com.snowcattle.game.db.common.annotation.DbMapper;
import com.snowcattle.game.db.common.annotation.EntitySave;
import com.snowcattle.game.db.common.annotation.FieldSave;
Expand All @@ -11,7 +13,7 @@

@EntitySave
@DbMapper(mapper = OrderMapper.class)
public class Order extends BaseEntity implements RedisInterface{
public class Order extends BaseEntity implements RedisInterface, AsyncCacheKey{

@FieldSave
private String status;
Expand Down Expand Up @@ -47,6 +49,11 @@ public String getUniqueKey() {

@Override
public String getRedisKeyEnumString() {
return "od#";
return RedisKeyEnum.PLAYER.getKey();
}

@Override
public String getAsyncCacheKey() {
return RedisKeyEnum.ASYNC_PLAYER.getKey();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.snowcattle.game.db.service.jdbc.test;

import com.snowcattle.game.db.service.jdbc.entity.Order;
import com.snowcattle.game.db.service.jdbc.service.impl.OrderService;
import com.snowcattle.game.db.service.proxy.EntityAysncServiceProxyFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
* Created by jiangwenping on 17/3/29.
*/
public class JdbcAsyncCacheTest {
public static long userId = 99999;

public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext(new String[]{"bean/db_applicationContext.xml"});
OrderService orderService = getOrderProxyService(classPathXmlApplicationContext);
// insertTest(classPathXmlApplicationContext, orderService);
// insertBatchTest(classPathXmlApplicationContext, orderService);
// Order order = getTest(classPathXmlApplicationContext, orderService);
// List<Order> orderList = getOrderList(classPathXmlApplicationContext, orderService);
// updateTest(classPathXmlApplicationContext, orderService, order);
// updateBatchTest(classPathXmlApplicationContext, orderService, orderList);
// deleteTest(classPathXmlApplicationContext, orderService, order);
// deleteBatchTest(classPathXmlApplicationContext, orderService, orderList);
// getListTest(classPathXmlApplicationContext, orderService);

}


public static void deleteBatchTest(ClassPathXmlApplicationContext classPathXmlApplicationContext, OrderService orderService, List<Order> orderList) throws Exception {
JdbcTest.deleteBatchTest(classPathXmlApplicationContext, orderService, orderList);
}

public static void updateBatchTest(ClassPathXmlApplicationContext classPathXmlApplicationContext, OrderService orderService, List<Order> orderList) throws Exception {
JdbcTest.updateBatchTest(classPathXmlApplicationContext, orderService, orderList);
}

public static void insertBatchTest(ClassPathXmlApplicationContext classPathXmlApplicationContext, OrderService orderService) throws Exception {
JdbcTest.insertBatchTest(classPathXmlApplicationContext, orderService);
}

public static List<Order> getOrderList(ClassPathXmlApplicationContext classPathXmlApplicationContext, OrderService orderService) throws Exception {
return JdbcTest.getOrderList(classPathXmlApplicationContext, orderService);
}

public static void insertTest(ClassPathXmlApplicationContext classPathXmlApplicationContext, OrderService orderService) {
JdbcTest.insertTest(classPathXmlApplicationContext, orderService);
}

public static Order getTest(ClassPathXmlApplicationContext classPathXmlApplicationContext, OrderService orderService) {
return JdbcTest.getTest(classPathXmlApplicationContext, orderService);
}

public static void getListTest(ClassPathXmlApplicationContext classPathXmlApplicationContext, OrderService orderService) {
JdbcTest.getListTest(classPathXmlApplicationContext, orderService);
}


public static void updateTest(ClassPathXmlApplicationContext classPathXmlApplicationContext, OrderService orderService, Order order) throws Exception {
JdbcTest.updateTest(classPathXmlApplicationContext, orderService, order);
}

public static void deleteTest(ClassPathXmlApplicationContext classPathXmlApplicationContext, OrderService orderService, Order order) throws Exception {
JdbcTest.deleteTest(classPathXmlApplicationContext, orderService, order);
}

public static OrderService getOrderProxyService(ClassPathXmlApplicationContext classPathXmlApplicationContext) throws Exception {
OrderService orderService = (OrderService) classPathXmlApplicationContext.getBean("orderService");
EntityAysncServiceProxyFactory entityAysncServiceProxyFactory = (EntityAysncServiceProxyFactory) classPathXmlApplicationContext.getBean("entityAysncServiceProxyFactory");
orderService = entityAysncServiceProxyFactory.createProxyService(orderService);
return orderService;
}
}

0 comments on commit 655089f

Please sign in to comment.