Rails 以编程方式检测文件更改

发布于 2024-11-27 15:53:35 字数 354 浏览 0 评论 0原文

我想编写一种方法,以编程方式检测 Rails 应用程序中的任何文件是否已更改。是否可以执行整个应用程序的 MD5 之类的操作并将其存储在会话变量中?

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

更新

File.ctime(".") 是完美的,除了“.”。当更深层次的目录文件发生更改时,不会标记为已更改。

迭代“.”中的所有目录是否有意义?并将每个的 ctime 加在一起?

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?

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).

Update

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满栀 2024-12-04 15:53:35

您是否考虑过使用 Guard

每当项目中的文件发生更改时,您都可以以编程方式执行任何操作。

有一个关于的精彩railscast

Have you considered using Guard.

You can programatically do anything whenever a file in your project changes.

There is a nice railscast about it

暖风昔人 2024-12-04 15:53:35

有一个简单的 ruby​​ gem,称为 filewatcher。这是最高级的例子:

require 'filewatcher'

FileWatcher.new(["README.rdoc"]).watch() do |filename, event|
  if(event == :changed)
    puts "File updated: " + filename
  end
  if(event == :delete)
    puts "File deleted: " + filename
  end
  if(event == :new)
    puts "New file: " + filename
  end
end

There is a simple ruby gem called filewatcher. This is the most advanced example:

require 'filewatcher'

FileWatcher.new(["README.rdoc"]).watch() do |filename, event|
  if(event == :changed)
    puts "File updated: " + filename
  end
  if(event == :delete)
    puts "File deleted: " + filename
  end
  if(event == :new)
    puts "New file: " + filename
  end
end
沒落の蓅哖 2024-12-04 15:53:35

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文