小编Nik*_*las的帖子

RedisTemplate过期不起作用

我正在尝试测试 RedisTemplate 中的过期方法。例如,我将会话存储在 redis 中,然后尝试检索会话并检查值是否相同。对于过期会话,我使用 redisTemplate 的 expire() 方法,对于获取过期会话,我使用 getExpire() 方法。但这不起作用。如何测试存储在 Redis 中的值?

//without import and fields
public class Cache() {     

    private StringRedisTemplate redisTemplate;

    public boolean expireSession(String session, int duration) {
      return redisTemplate.expire(session, duration, TimeUnit.MINUTES);    
    } 
}

//Test class without imports and fields 
public class TestCache() {  

    private Cache cache = new Cache(); 
    @Test
    public void testExpireSession() {
        Integer duration = 16;
        String session = "SESSION_123";
        cache.expireSession(session, duration);
        assertEquals(redisTemplate.getExpire(session, TimeUnit.MINUTES), Long.valueOf(duration));    
    }    
}
Run Code Online (Sandbox Code Playgroud)

但测试失败并出现 AssertionError:

预计:16 实际:0

更新: 我想, getExpire() …

java session unit-testing redis

3
推荐指数
1
解决办法
2万
查看次数

我应该使用AbstractAnnotationConfigDispatcherServletInitializer或WebApplicationInitializer配置Spring 4.0 mvc + Security应用程序吗?

我读了例子并且无法理解,我应该使用WebApplicationInitializer或AbstractAnnotationConfigDispatcherServletInitializer来配置我的spring应用程序吗?

java spring config spring-mvc spring-security

2
推荐指数
1
解决办法
2248
查看次数

自动装配后 Spring 更改 Map 键

我有一个配置类,我可以像这样将 Map 创建为 Bean。

@Bean
public Map<String, Filter> filters() {
    Map<String, Filter> map = new HashMap<>();
    map.put("RECOMMENDATION", new RecommendationFilter());
    map.put("UPCOMING", new UpcomingFilter());
    return map;
}
Run Code Online (Sandbox Code Playgroud)

并在另一个服务中自动装配此地图:

@Autowired
private Map<String, Filter> filterMap;
Run Code Online (Sandbox Code Playgroud)

在调试模式下,我可以通过类名称看到映射有另一个键:

  • 推荐过滤器
  • 即将到来的过滤器

但不是“推荐”和“即将到来”,它是如何在配置中设置的。而且谷歌搜索没有帮助。

java spring dictionary javabeans autowired

2
推荐指数
1
解决办法
553
查看次数

InputStream 在 spring 控制器中关闭

使用 spring 控制器,端点在主体响应中返回文件。我想确保不要使用“尝试使用资源”来避免资源泄漏,但在邮递员中我会收到错误:

“错误”:“内部服务器错误”,“消息”:“流已关闭”,

Spring控制器中的代码片段:

InputStreamResource result;
ResponseEntity<Resource> response;
try(FileInputStream ios = new FileInputStream(file)){
    result = new InputStreamResource(ios);
    response = ResponseEntity.ok()
        .headers(/*some headers here*/)
        .contentLength(file.length())
        .contentType(/*some media type here*/)
        .body(result);
    logger.info("successfully created");
    return response;
} catch (IOException e) {
        //some code here..
}
Run Code Online (Sandbox Code Playgroud)

有趣的是,在日志中我收到了成功消息,但在邮递员或浏览器中(这是一个 GET 请求)我收到了错误。

如果不使用“try-with-resource”,它会起作用,但我担心这种方式会导致资源泄漏。

java stream try-with-resources spring-restcontroller

2
推荐指数
1
解决办法
6318
查看次数

如何使用嵌入式python脚本编译和运行C代码?

我正在使用python 文档中的这个例子

#include <Python.h>
int
main(int argc, char *argv[])
{
  Py_SetProgramName(argv[0]);  /* optional but recommended */
  Py_Initialize();
  PyRun_SimpleString("from time import time,ctime\n"
                     "print('Today is', ctime(time()))\n");
  Py_Finalize();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

其中 python 脚本被硬编码为 C 程序。但是当我尝试编译它时

$ gcc -c modwithpy.c -o mod
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

modwithpy.c:1:20: 致命错误: Python.h: 没有这样的文件或目录编译终止。

但是,我已经安装了python-dev包。我还查看了编译和链接文档,但不明白我需要编写的 python 包的绝对路径。

$ whereis python
python: /usr/bin/python3.3m /usr/bin/python /usr/bin/python2.7-config 
/usr/bin/python3.3 /usr/bin/python2.7 /etc/python /etc/python3.3 /etc/python2.7
/usr/lib/python2.6 /usr/lib/python3.3 /usr/lib/python2.7 /usr/bin/X11/python3.3m
/usr/bin/X11/python /usr/bin/X11/python2.7-config /usr/bin/X11/python3.3
/usr/bin/X11/python2.7 /usr/local/lib/python3.3 /usr/local/lib/python2.7
/usr/include/python2.7 /usr/share/python /usr/share/man/man1/python.1.gz
Run Code Online (Sandbox Code Playgroud)

c python

1
推荐指数
1
解决办法
1729
查看次数