Jump to content

Archived

This topic is now archived and is closed to further replies.

dgoosens

Zend_Cache and Zend_Service_Flickr

Recommended Posts

hi freaks,

 

This is my situation... I am trying to dive into Zend Framework and to do so I got Zend Framework in Action by Rob ALLEN.

 

I am now playing around with Zend_Services, namely Zend_Service_Flickr. Everything works great, getting the images etc.

But, as this is rather heavy, I'd need to cache my Flickr result.

 

This is my Flickr class:

class Flickr {

    protected $apikey;

    public function __construct() {

        $this->apikey = Zend_Registry::get('config')->flickr_api_key;
    }

    public function search($keywords, $amount = 6) {

        if(empty ($this->apikey)) {
            return null;
        }

        try {
            $flickr = new Zend_Service_Flickr($this->apikey);
            $results = $flickr->tagSearch($keywords, 
                    array(
                        'per_page' => $amount,
                        'tag_mode' => 'all',
                        'license' => 3
                    ));

            if($results->totalResults() > 0) {
                return $results;
            }

        } catch (Zend_Service_Exception $e) {
            return null;
        }

        return null;
    }
}

 

and this is the part of my controller that should deal with caching the result:

      $frontendOptions = array(
            'lifetime' => 300,
            'automatic_serialization' => true
        );

        $backendOptions = array(
            'cache_dir' => ROOT_DIR . '/var/cache/'
        );
        
        $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
        if(!$results = $cache->load('flickr')) {
            $flickr = new Flickr;
            $results = $flickr->search($article->keywords);

            if($results != null) {
                $cache->save($results, 'flickr');

                
            }
        }

        $this->view->flickr = $results;

 

When there is no cache, the images show up without any problem.

In my cache directory, two files are created:

- zend_cache---flickr

- zend_cache---internal-metadatas---flickr

 

However, when reloading my page, and thus trying to get the data out of the cache, no images show up.

When I print_r($this->result) in my view, I allways get the same array:

Zend_Service_Flickr_ResultSet Object
(
    [totalResultsAvailable] => 139778
    [totalResultsReturned] => 6
    [firstResultPosition] => 1
    [_results:protected] => DOMNodeList Object
        (
        )

    [_flickr:private] => Zend_Service_Flickr Object
        (
            [apiKey] => XXXX_API_KEY_XXXX
            [_restClient:protected] => Zend_Rest_Client Object
                (
                    [_data:protected] => Array
                        (
                        )

                    [_uri:protected] => Zend_Uri_Http Object
                        (
                            [_username:protected] => 
                            [_password:protected] => 
                            [_host:protected] => www.flickr.com
                            [_port:protected] => 80
                            [_path:protected] => /services/rest/
                            [_query:protected] => 
                            [_fragment:protected] => 
                            [_regex:protected] => Array
                                (
                                    [escaped] => %[[:xdigit:]]{2}
                                    [unreserved] => [A-Za-z0-9-_.!~*'()\[\]]
                                    [segment] => (?:%[[:xdigit:]]{2}|[A-Za-z0-9-_.!~*'()\[\]:@&=+$,;])*
                                    [path] => (?:\/(??:%[[:xdigit:]]{2}|[A-Za-z0-9-_.!~*'()\[\]:@&=+$,;])*)?)+
                                    [uric] => (?:%[[:xdigit:]]{2}|[A-Za-z0-9-_.!~*'()\[\];\/?:@&=+$,])
                                )

                            [_scheme:protected] => http
                        )

                )

        )

    [_currentIndex:private] => 0
)

... but the images just don't show up.

 

Now, I am figuring that there might be some kind of problem with the serialization/unserialization of the Flickr object.

Zend_Cache works fine - tried a string and an array.

 

Any clues why the images just don't show up ?

Thanks a lot in advance freaks !

dGo

Share this post


Link to post
Share on other sites

I see _results is a DOMNodeList. That is an internal structure that wont serialize. You'll have to fetch the results in the resultset into a serializable composite, like an array or object of a userland class.

Share this post


Link to post
Share on other sites

thanks a lot for your reply 448191

 

So, if I am getting this right, I should, within my Flickr class, convert the $result into an array... Would that work ?

 

I guess I could iterate through the DOMNodeList and insert all the data into an array... or would there be some toArray() method that I could use for this ?

Share this post


Link to post
Share on other sites

thanks a lot for your reply 448191

 

So, if I am getting this right, I should, within my Flickr class, convert the $result into an array... Would that work ?

 

I guess I could iterate through the DOMNodeList and insert all the data into an array... or would there be some toArray() method that I could use for this ?

 

Yes and I don't know. But I assume you're smart enough to figure it out from here, if not you can send me a PM and we can discuss my hourly rate.

Share this post


Link to post
Share on other sites

[...]if not you can send me a PM and we can discuss my hourly rate.

 

I was only asking for advice... I thought this was the purpose of this forum...

I did not ask you to write one single line of code did I...

 

but thanks anyway...

Share this post


Link to post
Share on other sites

It was a joke. Meant to illustrate that since I don't know the specific API heart, and you are currently working on it, you looking it up would be quicker than me looking it up and you waiting until I have an answer.

Share this post


Link to post
Share on other sites

oh OK...

 

lol - sorry...

I am trying to figure this out while doing the job that I am payed for, which has nothing to do with PHP and is rather boring right now...

 

I'll post my solution when I'll have one.

Share this post


Link to post
Share on other sites

well...

finally got to the real business... (not at work anymore)

 

Thus, I changed the $result of my Flickr class into an array and now the Zend_Cache is working... and serializing the data correctly.

Here is my new class:

class Flickr {

    protected $apikey;

    public function __construct() {

        $this->apikey = Zend_Registry::get('config')->flickr_api_key;
    }

    public function search($keywords, $amount = 6) {

        if(empty ($this->apikey)) {
            return null;
        }

        try {
            $flickr = new Zend_Service_Flickr($this->apikey);
            $results = $flickr->tagSearch($keywords, 
                    array(
                        'per_page' => $amount,
                        'tag_mode' => 'all',
                        'license' => 3
                    ));

            if($results->totalResults() > 0) {
                foreach ($results as $key => $value) {
                    $resultsArray[$key] = $value;
                }
                return $resultsArray;
            }

            

        } catch (Zend_Service_Exception $e) {
            return null;
        }

        return null;
    }
}

 

I just had to change one line in my view... but it is working great now.

 

Thanks a lot for your help 448191 !

 

dGo

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.