Skip to content

Commit

Permalink
Item13039: added caching
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDaum committed Sep 26, 2014
1 parent 42d5bc7 commit 8f42c86
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 7 deletions.
5 changes: 3 additions & 2 deletions data/System/OEmbedPlugin.txt
@@ -1,4 +1,4 @@
%META:TOPICINFO{author="ProjectContributor" comment="" date="1392223960" format="1.1" version="3"}%
%META:TOPICINFO{author="ProjectContributor" comment="" date="1411729022" format="1.1" version="1"}%
---+!! %TOPIC%
%SHORTDESCRIPTION%

Expand Down Expand Up @@ -229,11 +229,12 @@ See the =oembed.tmpl= file on how to use them.
-->

| Author(s): | Michael Daum|
| Copyright: | © 2013 Michael Daum http://michaeldaumconsulting.com |
| Copyright: | © 2013-2014 Michael Daum http://michaeldaumconsulting.com |
| License: | [[http://www.gnu.org/licenses/gpl.html][GPL (Gnu General Public License)]] |
| Release: | %$RELEASE% |
| Version: | %$VERSION% |
| Change History: | <!-- versions below in reverse order -->&nbsp; |
| 25 Sep 2014 | added caching |
| 24 Sep 2014 | added a custom renderer for youtube videos |
| 29 Aug 2014 | removed my opera, qirk and urtak; \
use https whereever possible |
Expand Down
10 changes: 8 additions & 2 deletions lib/Foswiki/Plugins/OEmbedPlugin.pm
Expand Up @@ -20,8 +20,8 @@ use warnings;

use Foswiki::Func ();

our $VERSION = '3.01';
our $RELEASE = '3.01';
our $VERSION = '4.00';
our $RELEASE = '4.00';
our $SHORTDESCRIPTION = 'Easy embedding of third party content';
our $NO_PREFS_IN_TOPIC = 1;
our $core;
Expand All @@ -44,6 +44,12 @@ sub initPlugin {
Foswiki::Func::registerTagHandler('OEMBED', sub { return core->EMBED(@_); });
Foswiki::Func::registerTagHandler('EMBED', sub { return core->EMBED(@_); });

Foswiki::Func::registerRESTHandler('purgeCache', sub { return core->purgeCache(@_); },
authenticate => 0,
validate => 0,
http_allow => 'GET,POST',
);

return 1;
}

Expand Down
93 changes: 93 additions & 0 deletions lib/Foswiki/Plugins/OEmbedPlugin/Consumer.pm
@@ -0,0 +1,93 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# OEmbedPlugin is Copyright (C) 2013-2014 Michael Daum http://michaeldaumconsulting.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details, published at
# http://www.gnu.org/copyleft/gpl.html

package Foswiki::Plugins::OEmbedPlugin::Consumer;

use strict;
use warnings;

use Web::oEmbed ();
use Foswiki::Func ();
use Storable ();
our @ISA = qw( Web::oEmbed );

sub new {
my $class = shift;

my $this = bless($class->SUPER::new(@_), $class);

my $workingDir = Foswiki::Func::getWorkArea('OEmbedPlugin');
$this->{cacheRoot} = $workingDir . '/cache';
$this->{cacheExpire} = "1 d";

return $this;
}

sub embed {
my ($this, $uri, $opt) = @_;

my $key = _cache_key("".$uri);

my $url = $this->request_url($uri, $opt) or return;

my $cache = $this->_cache;

my $res = $cache->get($key);

if ($res) {
$res = Storable::thaw($res);
} else {
$res = Storable::thaw($res);
$res = $this->agent->get($url);
$cache->set($key, Storable::freeze($res));
}

return Web::oEmbed::Response->new_from_response($res, $uri);
}

sub purgeCache {
my $this = shift;

$this->_cache->purge;
}

sub _cache {
my $this = shift;

unless (defined $this->{cache}) {
require Cache::FileCache;
$this->{cache} = Cache::FileCache->new({
cache_root => $this->{cacheRoot},
default_expires_in => $this->{cacheExpire},
}
);
}

return $this->{cache};
}

sub _cache_key {
return _untaint(Digest::MD5::md5_hex($_[0]));
}

sub _untaint {
my $content = shift;
if (defined $content && $content =~ /^(.*)$/s) {
$content = $1;
}
return $content;
}

1;
12 changes: 9 additions & 3 deletions lib/Foswiki/Plugins/OEmbedPlugin/Core.pm
@@ -1,6 +1,6 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# OEmbedPlugin is Copyright (C) 2013 Michael Daum http://michaeldaumconsulting.com
# OEmbedPlugin is Copyright (C) 2013-2014 Michael Daum http://michaeldaumconsulting.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
Expand All @@ -18,8 +18,8 @@ package Foswiki::Plugins::OEmbedPlugin::Core;
use strict;
use warnings;

use Web::oEmbed ();
use Foswiki::Func ();
use Foswiki::Plugins::OEmbedPlugin::Consumer ();
#use Data::Dump qw(dump);

use constant TRACE => 0; # toggle me
Expand All @@ -33,7 +33,7 @@ sub new {

my $this = bless({@_}, $class);

$this->{consumer} = Web::oEmbed->new({format => 'json'});
$this->{consumer} = Foswiki::Plugins::OEmbedPlugin::Consumer->new({format => 'json'});

if (defined $Foswiki::cfg{OEmbedPlugin}{Providers}) {

Expand Down Expand Up @@ -185,4 +185,10 @@ sub EMBED {
return $result;
}

sub purgeCache {
my $this = shift;

$this->{consumer}->purgeCache;
}

1;
1 change: 1 addition & 0 deletions lib/Foswiki/Plugins/OEmbedPlugin/MANIFEST
@@ -1,6 +1,7 @@
data/System/OEmbedPlugin.txt 0644
lib/CPAN/lib/Regexp/Assemble.pm 0644
lib/Foswiki/Plugins/OEmbedPlugin/Config.spec 0644
lib/Foswiki/Plugins/OEmbedPlugin/Consumer.pm 0644
lib/Foswiki/Plugins/OEmbedPlugin/Core.pm 0644
lib/Foswiki/Plugins/OEmbedPlugin.pm 0644
lib/Web/oEmbed.pm 0644
Expand Down

0 comments on commit 8f42c86

Please sign in to comment.