设为首页 收藏本站
查看: 1096|回复: 0

[经验分享] Java使用memcache示例

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-2-25 08:41:35 | 显示全部楼层 |阅读模式
Windows系统下memcache的安装
cmd进入到memcache目录下(win10需要以管理员身份运行cmd)
wKiom1bNe2Cx42PGAAApgb_2llw067.jpg
memcache.exe -d install
memcache.exe -d start
启动memcache服务后可以在windows服务中看到名称为memcache 的服务

Java使用memcache
项目目录如下
wKiom1bNewzyQqotAABhndBm_zg860.jpg
User.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class User implements Serializable{
    private static final long serialVersionUID = 1L;

    private String username;
      
    private String password;

    //省略构造函数级getter setter   
     
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((password == null) ? 0 : password.hashCode());
        result = prime * result
                + ((username == null) ? 0 : username.hashCode());
        return result;
    }
  
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        User other = (User) obj;
        if (password == null) {
            if (other.password != null)
                return false;
        } else if (!password.equals(other.password))
            return false;
        if (username == null) {
            if (other.username != null)
                return false;
        } else if (!username.equals(other.username))
            return false;
        return true;
    }
}




MemcachedUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
public class MemcachedUtil {
    /**
     * memcached客户端单例
     */
    private static MemCachedClient cachedClient = new MemCachedClient();
    /**
     * 初始化连接池
     */
    static {
        //获取连接池的实例
        SockIOPool pool = SockIOPool.getInstance();
         
        //服务器列表及其权重
        String[] servers = {"127.0.0.1:11211"};
        Integer[] weights = {3};
         
        //设置服务器信息
        pool.setServers(servers);
        pool.setWeights(weights);
         
        //设置初始连接数、最小连接数、最大连接数、最大处理时间
        pool.setInitConn(10);
        pool.setMinConn(10);
        pool.setMaxConn(1000);
        pool.setMaxIdle(1000*60*60);
         
        //设置连接池守护线程的睡眠时间
        pool.setMaintSleep(60);
         
        //设置TCP参数,连接超时
        pool.setNagle(false);
        pool.setSocketTO(60);
        pool.setSocketConnectTO(0);
         
        //初始化并启动连接池
        pool.initialize();
         
        //压缩设置,超过指定大小的都压缩
//      cachedClient.setCompressEnable(true);
//      cachedClient.setCompressThreshold(1024*1024);
    }
      
    private MemcachedUtil(){
    }
      
    public static boolean add(String key, Object value) {
        return cachedClient.add(key, value);
    }
      
    public static boolean add(String key, Object value, Integer expire) {
        return cachedClient.add(key, value, expire);
    }
      
    public static boolean put(String key, Object value) {
        return cachedClient.set(key, value);
    }
      
    public static boolean put(String key, Object value, Integer expire) {
        return cachedClient.set(key, value, expire);
    }
      
    public static boolean replace(String key, Object value) {
        return cachedClient.replace(key, value);
    }
      
    public static boolean replace(String key, Object value, Integer expire) {
        return cachedClient.replace(key, value, expire);
    }
      
    public static Object get(String key) {
        return cachedClient.get(key);
    }
}




MemcachedUtilTest.java测试类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class MemcachedUtilTest {
    @Test
    public void testMemcached() {
        MemcachedUtil.put("hello", "world", 60);
        String hello = (String) MemcachedUtil.get("hello");
        Assert.assertEquals("world", hello);
         
        for(int i = 0; i < 10000; ++i) {
            User user = new User("Jason" + i, "123456-" + i);
            MemcachedUtil.put("user" + i, user, 60);
            Object obj = MemcachedUtil.get("user" + i);
            Assert.assertEquals(user, obj);
        }
    }
}




在Spring中配置并使用memcache
applicationContext.xml配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">
  
    <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool"
        factory-method="getInstance" init-method="initialize">
        <constructor-arg>
            <value>neeaMemcachedPool</value>
        </constructor-arg>
        <property name="servers">
            <list>
                <value>127.0.0.1:11211</value>
            </list>
        </property>
        <property name="initConn">
            <value>20</value>
        </property>
        <property name="minConn">
            <value>10</value>
        </property>
        <property name="maxConn">
            <value>50</value>
        </property>
        <property name="nagle">
            <value>false</value>
        </property>
        <property name="socketTO">
            <value>3000</value>
        </property>
    </bean>
    <bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient">
        <constructor-arg>
            <value>neeaMemcachedPool</value>
        </constructor-arg>
    </bean>
</beans>





MemcachedSpringTest.java测试类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class MemcachedSpringTest {
    private MemCachedClient cachedClient;
     
    @Before
    public void init() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        cachedClient = (MemCachedClient)context.getBean("memcachedClient");
    }
      
    @Test
    public void testMemcachedSpring() {
        User user = new User("lou", "jason");
        cachedClient.set("user", user);
        User cachedBean = (User)user;
        Assert.assertEquals(user, cachedBean);
    }
}





本机测试全部通过

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-182451-1-1.html 上篇帖子: 用memcache做session共享 下篇帖子: Memcached基本操作 Java
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表