Rails以编程方式检测文件的更改 [英] Rails detect changes to files programatically

查看:119
本文介绍了Rails以编程方式检测文件的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个方法,以编程方式检测我的rails应用程序中的任何文件是否已更改。是否可以做一些类似于整个应用程序的MD5并将其存储在会话变量中?

I would like to write a method that programatically detects whether any of the files in my rails app have been changed. Is it possible do do something like an MD5 of the whole app and store that in a session variable?

这主要是为了获得乐趣缓存清单。我已经有一个动态生成的缓存,它在生产中运行良好。但是在我的开发环境中,每当我更改app目录中的任何内容时,我都希望更新该缓存的id(而不是每10秒,这就是我现在设置它的方式)。

This is mostly for having some fun with cache manifest. I already have a dynamically generated cache and it works well in production. But in my dev environment, I would like the id of that cache to update whenever I change anything in the app directory (as opposed to every 10 seconds, which is how I have it setup right now).

更新

File.ctime(。)将是完美的,除了那个 。更深层目录文件发生更改时,未标记为已更改。

File.ctime(".") would be perfect, except that "." is not marked as having changed when deeper directory files have changed.

迭代。中的所有目录是否有意义。并将每个ctimes加在一起?

Does it make sense to iterate through all directories in "." and add together the ctimes for each?

推荐答案

File.ctime是关键。迭代所有文件并根据所有ctimes的总和创建一个唯一的id:

File.ctime is the key. Iterate through all files and create a unique id based on the sum of all their ctimes:

cache_id = 0
Dir.glob('./**/*') do |this_file|
   ignore_files = ['.', '..', "log"]
   ignore_files.each do |ig|
       next if this_file == ig
   end
   cache_id += File.ctime(this_file).to_i if File.directory?(this_file)
end

就像魅力,页面只在需要时重新缓存,即使在开发中也是如此。

Works like a charm, page only re-caches when it needs to, even in development.

这篇关于Rails以编程方式检测文件的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆