spring集成memcache 示例二

2024-04-23 09:32

本文主要是介绍spring集成memcache 示例二,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、前期准备

1)  下载memcached服务端memcached-1.2.6-win32-bin.zip,地址:http://code.jellycan.com/memcached/

2)  下载java版客户端 java_memcached-release_2.6.1.zip
3)  解压缩memcached-1.2.6-win32-bin.zip到指定目录,例如:D:\memcached-1.2.6-win32 ,在终端(即cmd命令行界面)
D:\memcached-1.2.6-win32\memcached.exe -d install
D:\memcached\memcached.exe -d start
这样memcache就会作为windows系统服务在每次开机时启动memcache服务。
常用命令
-p 监听的端口 
-l 连接的IP地址, 默认是本机 
-d start 启动memcached服务 
-d restart 重起memcached服务 
-d stop|shutdown 关闭正在运行的memcached服务 
-d install 安装memcached服务 
-d uninstall 卸载memcached服务 
-u 以的身份运行 (仅在以root运行的时候有效) 
-m 最大内存使用,单位MB。默认64MB 
-M 内存耗尽时返回错误,而不是删除项 
-c 最大同时连接数,默认是1024 
-f 块大小增长因子,默认是1.25 
-n 最小分配空间,key+value+flags默认是48 
-h 显示帮助 
二、实例代码
spring-memcache.xml  配置pool和memcache客户端
复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <beans xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:aop="http://www.springframework.org/schema/aop"
 7     xmlns:tx="http://www.springframework.org/schema/tx"
 8     xsi:schemaLocation="http://www.springframework.org/schema/beans
 9     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
10     http://www.springframework.org/schema/context
11     http://www.springframework.org/schema/context/spring-context-2.5.xsd
12     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
13     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
14 
15 
16     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
17         <property name="locations">
18             <list>
19                 <value>classpath:properties/memcache.properties</value>
20             </list>
21         </property>
22     </bean>
23 
24     <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool"
25         factory-method="getInstance" init-method="initialize"
26         destroy-method="shutDown">
27 
28         <constructor-arg>
29             <value>memCachedPool</value>
30         </constructor-arg>
31         
32         <property name="servers">
33             <list>
34                 <value>${memcache.server}</value>
35             </list>
36         </property>
37         
38         <property name="initConn">
39             <value>${memcache.initConn}</value>
40         </property>
41         
42         <property name="minConn">
43             <value>${memcache.minConn}</value>
44         </property>
45 
46         <property name="maxConn">
47             <value>${memcache.maxConn}</value>
48         </property>
49 
50         <property name="maintSleep">
51             <value>${memcache.maintSleep}</value>
52         </property>
53 
54         <property name="nagle">
55             <value>${memcache.nagle}</value>
56         </property>
57 
58         <property name="socketTO">
59             <value>${memcache.socketTO}</value>
60         </property>
61     </bean>
62 
63     <bean id="memCachedClient" class="com.danga.MemCached.MemCachedClient">
64         <constructor-arg>
65             <value>memCachedPool</value>
66         </constructor-arg>
67     </bean>
68 
69 </beans>
复制代码

 

memcache.properties memcache的连接属性 这是本机做服务器的,如果是其它机器,换ip 端口即可
复制代码
memcache.server=127.0.0.1:11211
memcache.initConn=20
memcache.minConn=10
memcache.maxConn=50
memcache.maintSleep=3000
memcache.nagle=false
memcache.socketTO=3000
复制代码

 

TestMemcache.java测试类   用的是junit4

复制代码
 1 package com.pis.memcache;
 2 
 3 import org.junit.Before;
 4 import org.junit.Test;
 5 import org.springframework.context.ApplicationContext;
 6 import org.springframework.context.support.ClassPathXmlApplicationContext;
 7 
 8 import com.danga.MemCached.MemCachedClient;
 9 
10 public class TestMemcache {
11     MemCachedClient memCachedClient;
12     @Before
13     public void beforeTest(){
14         
15         ApplicationContext atx = new ClassPathXmlApplicationContext("/spring/spring-memcache.xml");
16         memCachedClient = (MemCachedClient)atx.getBean("memCachedClient");
17     }
18     
19     
20     @Test
21     public void TestMem(){
22         memCachedClient.set("name", "han");
23         
24         System.out.println(memCachedClient.get("name"));
25     }
26     
27     
28     
29 }
欢迎加我的qq技术群425783133

这篇关于spring集成memcache 示例二的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/928417

相关文章

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

Python中yield的用法和实际应用示例

《Python中yield的用法和实际应用示例》在Python中,yield关键字主要用于生成器函数(generatorfunctions)中,其目的是使函数能够像迭代器一样工作,即可以被遍历,但不会... 目录python中yield的用法详解一、引言二、yield的基本用法1、yield与生成器2、yi

Springboot项目构建时各种依赖详细介绍与依赖关系说明详解

《Springboot项目构建时各种依赖详细介绍与依赖关系说明详解》SpringBoot通过spring-boot-dependencies统一依赖版本管理,spring-boot-starter-w... 目录一、spring-boot-dependencies1.简介2. 内容概览3.核心内容结构4.

Spring Boot 整合 SSE(Server-Sent Events)实战案例(全网最全)

《SpringBoot整合SSE(Server-SentEvents)实战案例(全网最全)》本文通过实战案例讲解SpringBoot整合SSE技术,涵盖实现原理、代码配置、异常处理及前端交互,... 目录Spring Boot 整合 SSE(Server-Sent Events)1、简述SSE与其他技术的对

Spring Security 前后端分离场景下的会话并发管理

《SpringSecurity前后端分离场景下的会话并发管理》本文介绍了在前后端分离架构下实现SpringSecurity会话并发管理的问题,传统Web开发中只需简单配置sessionManage... 目录背景分析传统 web 开发中的 sessionManagement 入口ConcurrentSess

Java整合Protocol Buffers实现高效数据序列化实践

《Java整合ProtocolBuffers实现高效数据序列化实践》ProtocolBuffers是Google开发的一种语言中立、平台中立、可扩展的结构化数据序列化机制,类似于XML但更小、更快... 目录一、Protocol Buffers简介1.1 什么是Protocol Buffers1.2 Pro

Java实现本地缓存的四种方法实现与对比

《Java实现本地缓存的四种方法实现与对比》本地缓存的优点就是速度非常快,没有网络消耗,本地缓存比如caffine,guavacache这些都是比较常用的,下面我们来看看这四种缓存的具体实现吧... 目录1、HashMap2、Guava Cache3、Caffeine4、Encache本地缓存比如 caff

MyBatis-Plus 与 Spring Boot 集成原理实战示例

《MyBatis-Plus与SpringBoot集成原理实战示例》MyBatis-Plus通过自动配置与核心组件集成SpringBoot实现零配置,提供分页、逻辑删除等插件化功能,增强MyBa... 目录 一、MyBATis-Plus 简介 二、集成方式(Spring Boot)1. 引入依赖 三、核心机制

MySQL设置密码复杂度策略的完整步骤(附代码示例)

《MySQL设置密码复杂度策略的完整步骤(附代码示例)》MySQL密码策略还可能包括密码复杂度的检查,如是否要求密码包含大写字母、小写字母、数字和特殊字符等,:本文主要介绍MySQL设置密码复杂度... 目录前言1. 使用 validate_password 插件1.1 启用 validate_passwo

Java高效实现Word转PDF的完整指南

《Java高效实现Word转PDF的完整指南》这篇文章主要为大家详细介绍了如何用Spire.DocforJava库实现Word到PDF文档的快速转换,并解析其转换选项的灵活配置技巧,希望对大家有所帮助... 目录方法一:三步实现核心功能方法二:高级选项配置性能优化建议方法补充ASPose 实现方案Libre