设为首页 收藏本站
查看: 1003|回复: 1

[经验分享] Windows下启用redis,使用Srping-redis做简单Java对象的存取

[复制链接]

尚未签到

发表于 2017-12-8 09:53:42 | 显示全部楼层 |阅读模式
  Redis官方并不支持windows平台,windows团队提供了开发环境的使用版本,使用默认配置,启动redis-server.exe即可。
  使用目录中的配置文件需要在CMD下把配置文件名作为参数启动
DSC0000.png

DSC0001.png

  启动后如图:可以看到读取的配置文件路径为/path/to/redis.conf,不在上述目录中
DSC0002.png

  工程目录结构:
DSC0003.png

  存取值测试
  maven依赖 pom.xml, spring的其它依赖已省略:




<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.6.RELEASE</version>
</dependency>
  redis客户端配置



# Redis settings
redis.host=127.0.0.1
redis.port=6379
redis.pass=

redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
redis.testOnReturn=true
  applicationContext.xml 配置:



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<context:component-scan base-package="com.von" />
<context:property-placeholder location="classpath:redis.properties" />


<bean id="redisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxActive}" />
<property name="maxWaitMillis" value="${redis.maxActive}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
<property name="testOnReturn" value="${redis.testOnReturn}" />
</bean>
<bean id="connectionFactoryJedis"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"></property>
<property name="port" value="${redis.port}"></property>
<property name="password" value="${redis.pass}"></property>
<property name="poolConfig" ref="redisPoolConfig"></property>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<constructor-arg ref="connectionFactoryJedis" />
</bean>
<bean id="jedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactoryJedis"></property>
<property name="keySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
</bean>
<bean id="blogDAO" class="com.von.dao.redis.impl.BlogDaoImpl" />

</beans>
  代码



package com.von.dao.redis;
import com.von.model.Blog;
public interface BlogDao {
public Blog getBlog(long id);
public void saveBlog(Blog bolg);

}


package com.von.dao.redis.impl;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import com.von.dao.redis.BlogDao;
import com.von.model.Blog;
public class BlogDaoImpl implements BlogDao {
@Autowired
protected RedisTemplate<Serializable, Serializable> redisTemplate;
public void saveBlog(final Blog blog) {
redisTemplate.execute(new RedisCallback<Object>() {
@SuppressWarnings({ "rawtypes", "unchecked" })
public Object doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer redisSerializer = redisTemplate.getValueSerializer();
redisSerializer.serialize(blog);
connection.set(redisTemplate.getStringSerializer().serialize("blog.id." + blog.getId()),
redisSerializer.serialize(blog));
// redisTemplate.getStringSerializer().serialize(blog.getContext()));
return null;
}
});
}
public Blog getBlog(final long id) {
return redisTemplate.execute(new RedisCallback<Blog>() {
public Blog doInRedis(RedisConnection connection) throws DataAccessException {
byte[] key = redisTemplate.getStringSerializer().serialize("blog.id." + id);
if (connection.exists(key)) {
byte[] value = connection.get(key);
// redisTemplate.getStringSerializer().deserialize(value);
                    Blog blog;
@SuppressWarnings("rawtypes")
RedisSerializer redisSerializer = redisTemplate.getValueSerializer();
blog = (Blog) redisSerializer.deserialize(value);
// blog.setContext(context);
// blog.setId(id);
return blog;
}
return null;
}
});
}
}
  model Blog.java
  对象序列化要求类集成 Serializable 接口,它就是一个标识,可以使用默认的1L;序列化和反序列化对象是要求这个标识一致,具体作用可查阅相关资料;例如:程序版本升级是可改变Serializable 值,已提醒低版本的类库进行升级。



package com.von.model;
import java.io.Serializable;
public class Blog implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private long id;
private String context;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
  测试代码:



package com.von.maintest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.von.dao.redis.BlogDao;
import com.von.model.Blog;
public class JedisTest {
public static void main(String[] args) {
ApplicationContext ac =  new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");
BlogDao blogDAO = (BlogDao)ac.getBean("blogDAO");
Blog bolg1 = new Blog();
bolg1.setId(3);
bolg1.setContext("context-test-中文");
blogDAO.saveBlog(bolg1);
Blog blog2 = blogDAO.getBlog(3);
System.out.println(blog2.getContext());
}
}

运维网声明 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-422064-1-1.html 上篇帖子: spring boot(二): spring boot+jdbctemplate+sql server 下篇帖子: windows下vagrant的安装使用
累计签到:16 天
连续签到:4 天
发表于 2017-12-8 10:02:03 | 显示全部楼层
感谢楼主分享··············

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

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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