码迷,mamicode.com
首页 > 系统相关 > 详细

memcached的简单使用

时间:2018-03-13 15:33:05      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:java   except   contex   ted   utils   --   bye   class   html   

1:安装memcached等:http://www.runoob.com/memcached/window-install-memcached.html  引用的网上

2:memcached与spring整合

技术分享图片

<!--配置memcached-->
    <!--<context:property-placeholder location="classpath:memcached.properties"/>-->
    <bean id="memcachedClient" class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean">
        <property name="servers" value="127.0.0.1:11211"/>
        <property name="weights" value="1"/>
        <property name="sessionLocator">
            <bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator" />
        </property>
        <property name="transcoder">
            <bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
        </property>
        <property name="bufferAllocator">
            <bean class="net.rubyeye.xmemcached.buffer.SimpleBufferAllocator" />
        </property>
    </bean>

    <bean id="memecachedAccess" class="com.station.util.MemecachedAccess">
        <property name="client" ref="memcachedClient"></property>
    </bean>

</beans>

3:自己写的实现类

package com.station.util;

import net.rubyeye.xmemcached.MemcachedClient;

/**
 * Created by Administrator on 2018/1/3 0003.
 */
public class MemecachedAccess {

    private MemcachedClient client;

    public MemcachedClient getClient() {
        return client;
    }

    public void setClient(MemcachedClient client) {
        this.client = client;
    }

    //添加方法  带时间参数
    public boolean put(String key, Integer time, String value) {
        try {
            boolean add = client.add(key, time, value);
            return add;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    //添加方法
    public boolean put(String key, Object value) {
        try {
            boolean add = client.add(key, 1 * 60 * 60, value);
            return add;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    // 获取方法
    public String getValue(String key) {
        try {
            String value = client.get(key);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // 获取方法
    public Object getResouce(String key) {
        try {
            Object value = client.get(key);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //删除所有

    public void flushAll() {
        try {
            client.flushAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

技术分享图片

技术分享图片

技术分享图片

有什么不对的,请对指正!

 

memcached的简单使用

标签:java   except   contex   ted   utils   --   bye   class   html   

原文地址:https://www.cnblogs.com/ping157019-/p/8556123.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!