Memcached和Spring集成开发

xml配置文件

<bean id="memcachedPool" class="com.danga.MemCached.SockIOPool"
        factory-method="getInstance" init-method="initialize" destroy-method="shutDown">
        <constructor-arg>
            <value>neeaMemcachedPool</value>
        </constructor-arg>
        <property name="servers">
            <list>
                <value>192.168.174.104:2222</value>
            </list>
        </property>
        <property name="initConn">
            <value>5</value>
        </property>
        <property name="minConn">
            <value>5</value>
        </property>
        <property name="maxConn">
            <value>250</value>
        </property>
        <property name="maintSleep">
            <value>30</value>
        </property>
        <property name="nagle">
            <value>false</value>
        </property>
        <property name="maxIdle">
            <value>6000</value>
        </property>
        <property name="socketTO">
            <value>3000</value>
        </property>
    </bean>
    <!--memcached client -->
    <bean id="memCachedClient" class="com.danga.MemCached.MemCachedClient">
        <constructor-arg>
            <value>neeaMemcachedPool</value>
        </constructor-arg>
    </bean>

 

JAVA调用

@Primary
@Repository
public class GoodsMemDao implements GoodsDAO{
    
    private final String MEM_PRE="goods_";
    
    @Autowired
    private GoodsMapperDAO goodsDao;
    
    @Autowired
    private MemCachedClient memClient;
    
    
    public void create(GoodsModel m) {
        goodsDao.create(m);
    }

    public void update(GoodsModel m) {
        goodsDao.update(m);
        
        Object obj=memClient.get(MEM_PRE+m.getUuid());
        if(obj!=null){
            memClient.set(MEM_PRE+m.getUuid(), m);
        }
        
    }

    public void delete(Integer uuid) {
        goodsDao.delete(uuid);
        Object obj=memClient.get(MEM_PRE+uuid);
        if(obj!=null){
            memClient.delete(MEM_PRE+uuid);
        }
    }

    public GoodsModel getByUuid(Integer uuid) {
        
        Object obj=memClient.get(MEM_PRE+uuid);
        if(obj !=null){
            return (GoodsModel) memClient.get(MEM_PRE+uuid);
        }
    
        GoodsModel goods=goodsDao.getByUuid(uuid);
        memClient.set(MEM_PRE+uuid, goods);
        
        return goods;
    }

}

 

猜你喜欢

转载自www.cnblogs.com/xiaoliangup/p/9236202.html