zoukankan      html  css  js  c++  java
  • spring-data-redis --简单的用spring-data-redis

    spring-data-redis序列化策略

    spring-data-redis提供了多种serializer策略,这对使用jedis的开发者而言,实在是非常便捷。sdr提供了4种内置的serializer:

    • JdkSerializationRedisSerializer:使用JDK的序列化手段(serializable接口,ObjectInputStrean,ObjectOutputStream),数据以字节流存储,jdk序列化和反序列化数据
    • StringRedisSerializer:字符串编码,数据以string存储
    • JacksonJsonRedisSerializer:json格式存储
    • OxmSerializer:xml格式存储

    其中JdkSerializationRedisSerializer和StringRedisSerializer是最基础的序列化策略,其中“JacksonJsonRedisSerializer”与“OxmSerializer”都是基于stirng存储,因此它们是较为“高级”的序列化(最终还是使用string解析以及构建java对象)。

    RedisTemplate中需要声明4种serializer,默认为“JdkSerializationRedisSerializer”:

    1) keySerializer :对于普通K-V操作时,key采取的序列化策略
    2) valueSerializer:value采取的序列化策略
    3) hashKeySerializer: 在hash数据结构中,hash-key的序列化策略
    4) hashValueSerializer:hash-value的序列化策略

    无论如何,建议key/hashKey采用StringRedisSerializer。 这redis服务端用命令行好查看 配置如下

    1、maven配置

    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    		<spring.version>4.1.1.RELEASE</spring.version>
    	</properties>
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context-support</artifactId>
    			<version>${spring.version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.data</groupId>
    			<artifactId>spring-data-redis</artifactId>
    			<version>1.4.1.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>redis.clients</groupId>
    			<artifactId>jedis</artifactId>
    			<version>2.6.0</version>
    			<type>jar</type>
    			<scope>compile</scope>
    		</dependency>
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>4.11</version>
    			<scope>test</scope>
    		</dependency>
    	</dependencies>

    2、spring xml配置文件

    配置jedis的缓冲池

    	<!-- 配置Jedis的 缓冲池 -->
    	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" p:maxTotal="32" p:maxIdle="6"
    		p:maxWaitMillis="15000">
    	</bean>
    配置ConnectionFactory
    <!-- 配置Jedis connection -->
    	<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
    		p:poolConfig-ref="jedisPoolConfig" p:hostName="127.0.0.1" p:port="6379" p:usePool="true">
    	</bean>
    创建RedisTemplate
    <!-- 配置 redisTemplate 利用Stringserializer -->
    	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connectionFactory-ref="connectionFactory">
    		<property name="defaultSerializer">
    			<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    		</property>
    		<!-- 使用string主要是key 在redis端用命令好读 不然默认的序列化没办法读 -->
    		<property name="keySerializer">
    			<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    		</property>
    		<property name="hashKeySerializer">
    			<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    		</property>
    	</bean>
     3、案例
     
    private static ApplicationContext context = null;
    	private static RedisTemplate redisTemplate=null;
    	@org.junit.BeforeClass
    	public static void BeforeClass() {
    		if (context == null) {
    			System.out.println("ctx inited....");
    			context = new ClassPathXmlApplicationContext("spring-context.xml");
    			redisTemplate = context.getBean("redisTemplate",RedisTemplate.class);
    		}
    	}
     

    1.String

    	@Test
    	public void testString(){
    		ValueOperations opsForValue = redisTemplate.opsForValue();
    		
    		opsForValue.set("string:name", "achuan");
    		opsForValue.set("string:id","1");
    		
    		Object name = opsForValue.get("string:name");
    		assertNotNull(name);
    		assertEquals("achuan", name.toString());
    	}

    2.List

      说到List  Redis 提供了类似于消息队列的操作,

            @Test
    	public void testList() {
    		ListOperations opsForList = redisTemplate.opsForList();
    
    		String keyName = "ListKey";
    		
    		redisTemplate.delete(keyName);
    
    		opsForList.leftPush(keyName, "zhangsan");
    		opsForList.leftPush(keyName, "lisi");
    		opsForList.leftPushAll(keyName, "wangwu", "zhaoliu", "qianqi");
    
    		Long size = opsForList.size(keyName);
    		System.out.println("size:" + size);
    		
    		for (long i = 0; i &lt; size; i++) {
    			Object value = opsForList.index(keyName,i);
    			System.out.println("i:"+i+",value:"+value.toString());
    		}
    		
    		Object leftPop = opsForList.leftPop(keyName);
    		System.out.println("after pop size:"+opsForList.size(keyName));
    	}

    3 Hashes (SETS 自己看看吧。。)

     

    今天就先到这里,Redis 还有很多东西,比如cache transcation 等等 看下目录结构

  • 相关阅读:
    Kubernetes-一文详解ServiceAccount与RBAC权限控制
    删除无用的docker镜像与容器
    How do I write one to many query in Dapper.Net?
    c# 使用反射Reflection的Emit实现动态创建元数据及可执行文件
    IE浏览器下bootStrap form-control input输入框不显示兼容性问题
    WPF控件从一个窗口移动到另一个窗口,特别适合实时刷新的
    添加/扫描显示二维码中的换行之【另类视野】
    各浏览器官方离线版下载地址
    CentOS挂载NTFS
    System.Data.SQLite.Core for .NET 5 Core manual reference
  • 原文地址:https://www.cnblogs.com/wych/p/4159385.html
Copyright © 2011-2022 走看看