热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

java使用memcache示例

Windows系统下memcache的安装cmd进入到memcache目录下(win10需要以管理员身份运行cmd)memcache.exe-dinst

Windows系统下memcache的安装

cmd进入到memcache目录下(win10需要以管理员身份运行cmd)

wKiom1bNe2Cx42PGAAApgb_2llw067.png

memcache.exe -d install

memcache.exe -d start

启动memcache服务后可以在windows服务中看到名称为memcache 的服务

 

Java使用memcache

项目目录如下

wKiom1bNewzyQqotAABhndBm_zg860.png

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 &#61; 0; i < 10000; &#43;&#43;i) {
            User user &#61; new User("Jason" &#43; i, "123456-" &#43; i);
            MemcachedUtil.put("user" &#43; i, user, 60);
            Object obj &#61; MemcachedUtil.get("user" &#43; 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&#61;"1.0" encoding&#61;"UTF-8"?>
<beans xmlns&#61;"http://www.springframework.org/schema/beans"
    xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation&#61;"http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">
  
    <bean id&#61;"memcachedPool" class&#61;"com.danga.MemCached.SockIOPool"
        factory-method&#61;"getInstance" init-method&#61;"initialize">
        <constructor-arg>
            <value>neeaMemcachedPoolvalue>
        constructor-arg>
        <property name&#61;"servers">
            <list>
                <value>127.0.0.1:11211value>
            list>
        property>
        <property name&#61;"initConn">
            <value>20value>
        property>
        <property name&#61;"minConn">
            <value>10value>
        property>
        <property name&#61;"maxConn">
            <value>50value>
        property>
        <property name&#61;"nagle">
            <value>falsevalue>
        property>
        <property name&#61;"socketTO">
            <value>3000value>
        property>
    bean>
    <bean id&#61;"memcachedClient" class&#61;"com.danga.MemCached.MemCachedClient">
        <constructor-arg>
            <value>neeaMemcachedPoolvalue>
        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;
     
    &#64;Before
    public void init() {
        ApplicationContext context &#61; new ClassPathXmlApplicationContext("applicationContext.xml");
        cachedClient &#61; (MemCachedClient)context.getBean("memcachedClient");
    }
      
    &#64;Test
    public void testMemcachedSpring() {
        User user &#61; new User("lou""jason");
        cachedClient.set("user", user);
        User cachedBean &#61; (User)user;
        Assert.assertEquals(user, cachedBean);
    }
}

 

本机测试全部通过&#xff0c;当然如果你打算深入学习的话&#xff0c;最好还是找一些相关的java视频教程来看看。

转:https://www.cnblogs.com/jinshiyill/p/5215750.html



推荐阅读
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 开发笔记:Java是如何读取和写入浏览器Cookies的
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Java是如何读取和写入浏览器Cookies的相关的知识,希望对你有一定的参考价值。首先我 ... [详细]
  • Java中包装类的设计原因以及操作方法
    本文主要介绍了Java中设计包装类的原因以及操作方法。在Java中,除了对象类型,还有八大基本类型,为了将基本类型转换成对象,Java引入了包装类。文章通过介绍包装类的定义和实现,解答了为什么需要包装类的问题,并提供了简单易用的操作方法。通过本文的学习,读者可以更好地理解和应用Java中的包装类。 ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • Android源码深入理解JNI技术的概述和应用
    本文介绍了Android源码中的JNI技术,包括概述和应用。JNI是Java Native Interface的缩写,是一种技术,可以实现Java程序调用Native语言写的函数,以及Native程序调用Java层的函数。在Android平台上,JNI充当了连接Java世界和Native世界的桥梁。本文通过分析Android源码中的相关文件和位置,深入探讨了JNI技术在Android开发中的重要性和应用场景。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
  • Java程序设计第4周学习总结及注释应用的开发笔记
    本文由编程笔记#小编为大家整理,主要介绍了201521123087《Java程序设计》第4周学习总结相关的知识,包括注释的应用和使用类的注释与方法的注释进行注释的方法,并在Eclipse中查看。摘要内容大约为150字,提供了一定的参考价值。 ... [详细]
author-avatar
esnard夏_368
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有