• 超简单使用MemCached


    阅读本文的前提是,你已经安装或者使用了MemCached,或具有相关的基本知识。

    今天要介绍的是Simple-Spring-Memcached,它封装了对MemCached的调用,使MemCached的客户端开发变得超乎寻常的简单,只要一行代码就行:

    @ReadThroughAssignCache(assignedKey = "VETS", expiration = 300, namespace = "NELZ")

    是不是很神奇?这行代码指定了MemCached的key,过期时间和命名空间。假设你的MemCached服务器IP是:196.168.10.101,端口是:12000,那么在数据调用的配置文件中只要加上下面配置代码就可以了:

    1 <import resource="classpath:simplesm-context.xml" />
    2
    3
    4
    5 <bean id="memcachedConnectionBean" class="net.nelz.simplesm.config.MemcachedConnectionBean">
    6
    7 <property name="consistentHashing" value="true" />
    8
    9 <property name="nodeList" value="196.168.10.101:12000" />
    10
    11 </bean>

    从simplesm-context.xml的内容中,可以看出它所封装的类和方法:

    1 <bean id="memcachedClientFactory" class="net.nelz.simplesm.config.MemcachedClientFactory" >
    2
    3 <property name="bean" ref="memcachedConnectionBean" />
    4
    5 </bean>
    6
    7
    8
    9 <bean id="memcachedClient" factory-bean="memcachedClientFactory" factory-method="createMemcachedClient" />
    10
    11
    12
    13 <bean id="methodStore" class="net.nelz.simplesm.aop.CacheKeyMethodStoreImpl" />
    14
    15
    16
    17 <bean id="net.nelz.simplesm.DefaultKeyProvider" class="net.nelz.simplesm.impl.DefaultKeyProvider">
    18
    19 <property name="methodStore" ref="methodStore" />
    20
    21 </bean>
    22
    23
    24
    25 <bean id="readThroughSingleCache" class="net.nelz.simplesm.aop.ReadThroughSingleCacheAdvice">
    26
    27 <property name="cache" ref="memcachedClient" />
    28
    29 <property name="methodStore" ref="methodStore" />
    30
    31 <property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
    32
    33 </bean>
    34
    35 <bean id="readThroughMultiCache" class="net.nelz.simplesm.aop.ReadThroughMultiCacheAdvice">
    36
    37 <property name="cache" ref="memcachedClient" />
    38
    39 <property name="methodStore" ref="methodStore" />
    40
    41 <property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
    42
    43 </bean>
    44
    45 <bean id="readThroughAssignCache" class="net.nelz.simplesm.aop.ReadThroughAssignCacheAdvice">
    46
    47 <property name="cache" ref="memcachedClient" />
    48
    49 <property name="methodStore" ref="methodStore" />
    50
    51 <property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
    52
    53 </bean>
    54
    55 <bean id="updateSingleCache" class="net.nelz.simplesm.aop.UpdateSingleCacheAdvice">
    56
    57 <property name="cache" ref="memcachedClient" />
    58
    59 <property name="methodStore" ref="methodStore" />
    60
    61 <property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
    62
    63 </bean>
    64
    65 <bean id="updateMultiCache" class="net.nelz.simplesm.aop.UpdateMultiCacheAdvice">
    66
    67 <property name="cache" ref="memcachedClient" />
    68
    69 <property name="methodStore" ref="methodStore" />
    70
    71 <property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
    72
    73 </bean>
    74
    75 <bean id="updateAssignCache" class="net.nelz.simplesm.aop.UpdateAssignCacheAdvice">
    76
    77 <property name="cache" ref="memcachedClient" />
    78
    79 <property name="methodStore" ref="methodStore" />
    80
    81 <property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
    82
    83 </bean>
    84
    85 <bean id="invalidateSingleCache" class="net.nelz.simplesm.aop.InvalidateSingleCacheAdvice">
    86
    87 <property name="cache" ref="memcachedClient" />
    88
    89 <property name="methodStore" ref="methodStore" />
    90
    91 <property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
    92
    93 </bean>
    94
    95 <bean id="invalidateMultiCache" class="net.nelz.simplesm.aop.InvalidateMultiCacheAdvice">
    96
    97 <property name="cache" ref="memcachedClient" />
    98
    99 <property name="methodStore" ref="methodStore" />
    100
    101 <property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
    102
    103 </bean>
    104
    105 <bean id="invalidateAssignCache" class="net.nelz.simplesm.aop.InvalidateAssignCacheAdvice">
    106
    107 <property name="cache" ref="memcachedClient" />
    108
    109 <property name="methodStore" ref="methodStore" />
    110
    111 <property name="defaultKeyProvider" ref="net.nelz.simplesm.DefaultKeyProvider" />
    112
    113 </bean>
    114
    115  

    Simple-Spring-Memcached还提供了一个例子,在spring的petClinic例子中加入了几行代码,就实现了对MemCached的调用:

    1 import net.nelz.simplesm.annotations.ReadThroughAssignCache;
    2
    3  import net.nelz.simplesm.annotations.ReadThroughSingleCache;
    4
    5 @ReadThroughAssignCache(assignedKey = "VETS", expiration = 300, namespace = "NELZ")
    6
    7 public Collection<Vet> getVets() {
    8
    9 System.out.println("\n ! ! !Gonna wait a bit: " + new Date() + "\n");
    10
    11 try {
    12
    13 Thread.sleep(4000);
    14
    15 } catch (Exception ex) {}
    16
    17 return sessionFactory.getCurrentSession().createQuery("from Vet vet order by vet.lastName, vet.firstName").list();
    18
    19 }
    20
    21  

    为了加强测试的效果,在第一次读取数据时,故意停顿了一下(sleep)。

    夜深了,大家也应该sleep了吧:)

  • 相关阅读:
    使用 PowerShell 进行 SQL Server 数据库备份
    当 Windows 10 应用商店更新游戏/应用遇到 0x80070032 错误时的一种解决方案
    在 SQL Server 恢复一个已存在的数据库的备份(即通过备份功能复制一个新数据库)
    在 Windows Server 2008 R2 中安装 PowerShell 5.1 与中文脚本错误解决
    vite项目使用 vitepluginmock 插件 mock数据,fetch报错SyntaxError: Unexpected token < in JSON at position 0
    MAC和移动端实现charles和代理软件共存【100%生效】
    eslint遇到的报错
    swiper.js轮播在iOS下禁用上拉下拉回弹问题解决
    js图片监听onload事件,依然有获取不到宽高的场景
    15分钟快速配置eslint,prettier,lintstaged,husky,commitizen实现前端项目代码规范化
  • 原文地址:https://www.cnblogs.com/chrischen662/p/1826397.html
Copyright © 2020-2023  润新知