Rails 以编程方式检测文件更改
我想编写一种方法,以编程方式检测 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否考虑过使用 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
有一个简单的 ruby gem,称为 filewatcher。这是最高级的例子:
There is a simple ruby gem called filewatcher. This is the most advanced example:
File.ctime 是关键。遍历所有文件并根据所有文件的 ctime 总和创建一个唯一的 id:
就像一个魅力一样,页面仅在需要时重新缓存,即使在开发中也是如此。
File.ctime is the key. Iterate through all files and create a unique id based on the sum of all their ctimes:
Works like a charm, page only re-caches when it needs to, even in development.