发表于: 2017-04-16 23:50:00

2 1649


今天完成的事情:

1.将Memcache替换成Redis,重复以步骤。最后生成一份压测报告,同样的发布到自媒体里

今天就这一条都没有完成。

首先安装redis,网上教程也比较多,按照教程走应该没什么问题:

打开一个 cmd 窗口 使用cd命令切换目录到 C:\redis 运行 redis-server.exe redis.windows.conf 。

这时候另启一个cmd窗口,原来的不要关闭,不然就无法访问服务端了。

切换到redis目录下运行 redis-cli.exe -h 127.0.0.1 -p 6379 。

设置键值对 set myKey abc

取出键值对 get myKey

这样redis就可以用了。

关于redis 网上大部分的教程都是讲redis直接操作,我认为这种用处也比较少。

2.关于在web项目中配置redis:

①redis.properties

#redis中心
#绑定的主机地址
redis.host=127.0.0.1
#指定Redis监听端口,默认端口为6379
redis.port=6379
#授权密码(本例子没有使用)
redis.password=123456  
#最大空闲数:空闲链接数大于maxIdle时,将进行回收
redis.maxIdle=100  
#最大连接数:能够同时建立的“最大链接个数”
redis.maxActive=300  
#最大等待时间:单位ms
redis.maxWait=1000  
#使用连接时,检测连接是否成功
redis.testOnBorrow=true
#当客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能
redis.timeout=10000 

②在ApplicationContext.xml配置redis

                      <!-- redis  conf   start   -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
   <property name="maxIdle" value="${redis.maxIdle}" />
   <!--<property name="maxActive" value="${redis.maxActive}"/>
   <property name="maxWait" value="${redis.maxWait}"/>-->
   <property name="maxTotal" value="${redis.maxActive}" />
   <property name="maxWaitMillis" value="${redis.maxWait}" />
   <property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>

<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
     p:host-name="${redis.host}"
     p:port="${redis.port}"
     p:password="${redis.password}"
     p:pool-config-ref="poolConfig" />
<bean id="stringSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<!-- 开启事务,可以通过transcational注解控制 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
   <property name="connectionFactory" ref="connectionFactory" />
   <property name="keySerializer" ref="stringSerializer" />
   <property name="enableTransactionSupport" value="true" />
</bean>
<!-- 对string操作的封装 -->
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"
     p:connection-factory-ref="connectionFactory" />
                   <!-- redis conf end-->

③关于redis的操作简直不要太多,看的头晕眼花。

网上的教程有关于spring+redis整合;mybatis+redis整合(利用mybatis缓存,感觉太复杂,放弃)

关于spring+redis整合又有诸多的操作

实验了两种都没成功:

一。

/*public class UserDaoImpl extends RedisGeneratorDao<String,User> implements UserDao {

   @Override
   public User findUserById(final String userName) {
       User result = (User) redisTemplate.execute(new RedisCallback<Object>() {
           @Override
           public User doInRedis(RedisConnection redisConnection) throws DataAccessException {
               RedisSerializer<String> serializer = getRedisSerializer();
               byte[] key = serializer.serialize(userName);
               byte[] value = redisConnection.get(key);
               if(value == null){
                   System.out.println("redis 缓存没有该对象");
                   return null;
               }
               String userAccount = serializer.deserialize(value);
               System.out.println("redis 缓存!!!!!有对象");
               return new User(userName,userAccount);
           }
       });
       return result;
   }

二。

@Override
public User findUserById(String userName) {
ValueOperations<String,User> valueOperations = redisTemplate.opsForValue();
   User user = valueOperations.get(userName);
   return user;
}

我更倾向于第二种方法,第一种方法api感觉太复杂了,而且操作对象的方法我还不会。

3.redis和memcache有一点比较大的不同就是:

memcache只需要在实体类上实现序列化接口即可,但是redis这样做还不够。redis保存的字符串对象的时候需要先进行序列化,当取出对象的时候也需要将redis缓存的字节转换成字符串。(当然,redis存储的类型更多,hash,列表,集合等)

今天在mooc上看了一段关于使用redis的视频,使用了Google的protocol,据说时间最快,占用内存最小。但是因为api不够友好,使用第三方开源的protobuff(在protocol上修改的)更方便。


等一会我再看一遍,看看能不能做出来。

今天的问题一直解决不了。


明天计划的事情:

1.完成任务六

2.开始任务八

遇到的问题:

1.

Caused by: java.lang.ClassNotFoundException: org.apache.commons.pool2.impl.GenericObjectPoolConfig

at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1892)

at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1735)

... 84 more

这个问题不知道怎么解决。

在网上查说是版本不对,但我按照给的版本修改了还是不行。

Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [redis.clients.jedis.JedisPoolConfig] for bean with name 'poolConfig' defined in class path resource [ApplicationContext.xml]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig

还有一个解决方法是:加一个依赖但是也不管用

<dependency>
    <groupId>org.apache.directory.studio</groupId>
    <artifactId>org.apache.commons.pool</artifactId>
    <version>1.6</version>
</dependency>



收获:

收获很多,对于redis感觉比memcache知道的还要多。



返回列表 返回列表
评论

    分享到