How to delete the browser cache through ruby

Hi All,

I am using the below code but it seems not working for IE. ( not sure).
Could anyone please help me how to delete the cache .

I also found the code in the below location but it seems very unclear
which method I need to call etc. Can anyone please step wise tell me if
I need to use the code from
http://lazax.com/software/del_cache/del_cache.html

http://lazax.com/software/del_cache/del_cache.html

def delete_cache

  @window_os_flag = RUBY_PLATFORM.downcase.include?("mswin")

  #@mac_os_flag = RUBY_PLATFORM.downcase.include?("darwin")

  @browser_name = page.get_eval("navigator.userAgent")

  if  @window_os_flag.to_s == "true"

    if @browser_name.to_s =~ /IE/
      puts "Test is running in window platform with browser IE"
      puts

Dir.glob(’/Users/’+ENV[‘USERNAME’]+’/AppData/Local/Microsoft/Windows/Temporary
Internet Files/’)
FileUtils.rm_r
Dir.glob(’/Users/’+ENV[‘USERNAME’]+’/AppData/Local/Microsoft/Windows/Temporary
Internet Files/
’),:force => true
puts
Dir.glob(’/Users/’+ENV[‘USERNAME’]+’/AppData/Local/Microsoft/Windows/Temporary
Internet Files/*’)
end

    if @browser_name.to_s =~ /Chrome/
      puts "Test is running in window platform with browser

googlechrome"
FileUtils.rm_r
Dir.glob(’/Users/’+ENV[‘USERNAME’]+’/AppData/Local/Google/Chrome/User
Data/Default/*’),:force => true
end

    if @browser_name.to_s =~ /Firefox/
      puts "Test is running in window platform with browser Firefox"
      FileUtils.rm_r

Dir.glob(’/Users/’+ENV[‘USERNAME’]+’/AppData/Local/Mozilla/Firefox/Profiles//Cache/’),:force
=> true
end

 end

end

Cheers
A

why you want to do that??
means why you want to clear cache…

I think it is probably a better idea to ensure that the cache expires in
due
time, rather than trying to clear it programmatically.


Thanks & Regards,
Dhruva S…

Dhruva S. wrote:

I think it is probably a better idea to ensure that the cache expires in
due
time, rather than trying to clear it programmatically.


Thanks & Regards,
Dhruva S…

Actually I am doing some performance testing by retrieving the response
time of the page without cache and with cache. So I need to delete the
cache.If anyone can help me for IE that would be great.

Cheers
Arihan

On 24/9/2010 6:41 PM, Arihan S. wrote:

cache.If anyone can help me for IE that would be great.
If your Ruby is running the client, you could try deleting the files
within the temporary files folder.

Cheers,
Mohit.
26/9/2010 | 12:49 AM.

Mohit S. wrote:

On 24/9/2010 6:41 PM, Arihan S. wrote:

cache.If anyone can help me for IE that would be great.
If your Ruby is running the client, you could try deleting the files
within the temporary files folder.

Cheers,
Mohit.
26/9/2010 | 12:49 AM.

Thats I am doing. if you chek the code what I’ve mentioned on the top
you can find that. but it seems not working for IE. I know some code
like as below mentioned in the net which I found but very confusing
which one I need to call

#!/bin/ruby

set TEMPIF=%USERPROFILE%\Local Settings\Temporary Internet Files

%TEMPIF%\Content.IE5\Index.DAT

require ‘Win32API’

HashMethods = a Hash that can be accessed with methods

e.g. h = MethodHash.new

h[‘street’] = ‘Broadway’

h.street = ‘Broadway’

puts h.street ===> Broadway

require ‘delegate’

class MethodHash < SimpleDelegator
def initialize h = {}
super h
end

def method_missing(method_name, *args)
    name = method_name.to_s
    if name.ends_with?('=')
        self[ name.chop ] = args[0]
    else
        self[ name ]
    end
end

end

class String
def ends_with?(substr)
len = substr.length
self.reverse() [0 … len-1].reverse == substr
end

def starts_with?(substr)
    len = substr.length
    self[0 .. len-1] == substr
end

alias start_with?  starts_with?
alias begin_with?  starts_with?
alias begins_with? starts_with?
alias end_with?    ends_with?

# String each() operator reads line-by-line
# These functions return characters
def each_char
    self.each_byte{|x| yield x.chr }
end
def collect_char
    r = []
    self.each_byte{|x| r << x.chr }
    r
end

end

=begin

// Windows System calls needed to clear cache.
//

BOOL DeleteUrlCacheEntry(
LPCTSTR lpszUrlName
);

HANDLE FindFirstUrlCacheEntry(
LPCTSTR lpszUrlSearchPattern,
LPINTERNET_CACHE_ENTRY_INFO lpFirstCacheEntryInfo,
LPDWORD lpdwFirstCacheEntryInfoBufferSize
);

BOOL FindNextUrlCacheEntry(
HANDLE hEnumHandle,
LPINTERNET_CACHE_ENTRY_INFO lpNextCacheEntryInfo,
LPWORD lpdwNextCacheEntryInfoBufferSize
);

BOOL FindCloseUrlCache(
IN HANDLE hEnumHandle
);

typedef struct _INTERNET_CACHE_ENTRY_INFO {
DWORD dwStructSize;
LPTSTR lpszSourceUrlName;
LPTSTR lpszLocalFileName;
DWORD CacheEntryType;
DWORD dwUseCount;
DWORD dwHitRate;
DWORD dwSizeLow;
DWORD dwSizeHigh;
FILETIME LastModifiedTime;
FILETIME ExpireTime;
FILETIME LastAccessTime;
FILETIME LastSyncTime;
LPBYTE lpHeaderInfo;
DWORD dwHeaderInfoSize;
LPTSTR lpszFileExtension;
union {
DWORD dwReserved;
DWORD dwExemptDelta;
}
} INTERNET_CACHE_ENTRY_INFO, *LPINTERNET_CACHE_ENTRY_INFO;

=end

def main
w = get_api(‘wininet’,FUNCS)
i = 0

info,infosize = get_first_info(w)
cache = w.FindFirstUrlCacheEntry.Call(nil,info,infosize)
if cache != 0
    begin
        len, source_file_ptr, local_file_ptr = info.unpack 'LLL'
        w.DeleteUrlCacheEntry.Call(source_file_ptr)
        i += 1
        info,infosize = get_next_info( w, cache )
    end while w.FindNextUrlCacheEntry.Call(cache, info, infosize) != 

0
end
w.FindCloseUrlCache.Call(cache)
i
end

def get_first_info(api)
sizenum = [0,0].pack(‘L*’)
buf = [1024,0].pack(‘L*’).ljust(1024)
r = api.FindFirstUrlCacheEntry.Call(nil,nil,sizenum)
n = sizenum.unpack(‘L’)[0]
info = sizenum.ljust(n)
[info,sizenum]
end

def get_next_info(api, handle)
sizenum = [0,0].pack(‘L*’)
buf = [1024,0].pack(‘L*’).ljust(1024)
r = api.FindNextUrlCacheEntry.Call(handle,nil,sizenum)
n = sizenum.unpack(‘L’)[0]
info = sizenum.ljust(n)
[info,sizenum]
end

Win32 API used in this file is listed here.

Each system call can be instatiated like this

DeleteUrlCacheEntry = Win32API.new(“wininet”,

“DeleteUrlCacheEntry”, [‘P’], ‘V’)

Instead, the functions are defined dynamically from this list:

FUNCS = {
‘FindFirstUrlCacheEntry’ => [‘ppp’,‘n’],
‘FindNextUrlCacheEntry’ => [‘npp’,‘n’],
‘DeleteUrlCacheEntry’ => [‘n’, ‘n’],
‘FindCloseUrlCache’ => [‘n’, ‘n’]
}

get_api returns a hash with Win32 API system calls

Usage:

api = get_api(‘Kernel32’, {‘GetLastError’=>[‘V’, ‘N’],…})

def get_api(library, function_hash)
f = MethodHash.new
function_hash.each{|funcname,types|
in_types = types[0].collect_char{|x| x}
out_type = types[1]
f[funcname] = Win32API.new(library, funcname, in_types,
out_type)
}
f
end

def getLastError
f = Win32API.new(‘Kernel32’, ‘GetLastError’, [‘V’], ‘N’)
f.call
end

main

#if FILE == $0

n = main

# print “Deleted #{n} items\n”

sleep 20

#end

On Fri, 24 Sep 2010 05:05:24 -0500, Arihan S.
[email protected] wrote in
[email protected]:

I am using the below code but it seems not working for IE. ( not sure).
Could anyone please help me how to delete the cache .

[snip]

   if @browser_name.to_s =~ /IE/
     puts "Test is running in window platform with browser IE"
     puts

Dir.glob(’/Users/’+ENV[‘USERNAME’]+’/AppData/Local/Microsoft/Windows/Temporary
Internet Files/’)
FileUtils.rm_r
Dir.glob(’/Users/’+ENV[‘USERNAME’]+’/AppData/Local/Microsoft/Windows/Temporary
Internet Files/
’),:force => true

It’s a bad idea to delete the temporary files at the filesytem level
like that. You could cause errors in the IE instance. Instead, Take
a look at the Win32API functions that you can use for this:
http://msdn.microsoft.com/en-us/library/aa384026(VS.85).aspx.

You can access these functions in Ruby using the Win32API library or
the dl/win32 library, both of which are part of the Ruby stdlib.

Thanks all.

I tried with sh %{ruby lib/del_cache.rb} and its working fine.

On Tue, 19 Oct 2010 14:33:08 -0500, Arihan S.
[email protected] wrote in
[email protected]:

   if @browser_name.to_s =~ /IE/

a look at the Win32API functions that you can use for this:
http://msdn.microsoft.com/en-us/library/aa384026(VS.85).aspx.

You can access these functions in Ruby using the Win32API library or
the dl/win32 library, both of which are part of the Ruby stdlib.

it seeems the whole delete_cache.rb is mentioned in this thread ( the
details code in the previous post) but its very confusing which method
need to call. So if you can highlight anything w.r.t that then it would
be great.

You should look at the method main starting on line 122. That’s the
loop that you’ll need to execute.

BTW, some of the method names (e.g. get_first_info) leave something to
be desired. If you’re going to use this code, even just as an
example, I’d rename those methods to something more descriptive.

Charles C. wrote in post #955502:

On Fri, 24 Sep 2010 05:05:24 -0500, Arihan S.
[email protected] wrote in
[email protected]:

I am using the below code but it seems not working for IE. ( not sure).
Could anyone please help me how to delete the cache .

[snip]

   if @browser_name.to_s =~ /IE/
     puts "Test is running in window platform with browser IE"
     puts

Dir.glob(’/Users/’+ENV[‘USERNAME’]+’/AppData/Local/Microsoft/Windows/Temporary
Internet Files/’)
FileUtils.rm_r
Dir.glob(’/Users/’+ENV[‘USERNAME’]+’/AppData/Local/Microsoft/Windows/Temporary
Internet Files/
’),:force => true

It’s a bad idea to delete the temporary files at the filesytem level
like that. You could cause errors in the IE instance. Instead, Take
a look at the Win32API functions that you can use for this:
http://msdn.microsoft.com/en-us/library/aa384026(VS.85).aspx.

You can access these functions in Ruby using the Win32API library or
the dl/win32 library, both of which are part of the Ruby stdlib.

Hi Charles,

it seeems the whole delete_cache.rb is mentioned in this thread ( the
details code in the previous post) but its very confusing which method
need to call. So if you can highlight anything w.r.t that then it would
be great.

Regards
Arihan

Charles C. wrote in post #956120:

On Tue, 19 Oct 2010 14:33:08 -0500, Arihan S.
[email protected] wrote in
[email protected]:

   if @browser_name.to_s =~ /IE/

a look at the Win32API functions that you can use for this:
http://msdn.microsoft.com/en-us/library/aa384026(VS.85).aspx.

You can access these functions in Ruby using the Win32API library or
the dl/win32 library, both of which are part of the Ruby stdlib.

it seeems the whole delete_cache.rb is mentioned in this thread ( the
details code in the previous post) but its very confusing which method
need to call. So if you can highlight anything w.r.t that then it would
be great.

You should look at the method main starting on line 122. That’s the
loop that you’ll need to execute.

BTW, some of the method names (e.g. get_first_info) leave something to
be desired. If you’re going to use this code, even just as an
example, I’d rename those methods to something more descriptive.

Hi Charles,

I did in a different way. I need to call that in my script so that when
I call, the complete del_cache.rb should run. So I used the below and
its working fine.

require ‘rake’
sh %{ruby lib/del_cache.rb}

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs